使用自己的库扩展CI

时间:2010-08-09 18:35:19

标签: php codeigniter

我正在尝试使用我计划用于计算货币时间价值的自定义库来扩展我的CI。问题是每当我加载类时出现内部服务器错误,我不太清楚为什么。另外,如果有任何其他建议,请告诉我。

由于

这是我正在尝试加载库的控制器。

class Timevalueshow extends Controller{

    $params = array('years' => 0, 
                    'rate' => 0, 
                    'principle' => 0, 
                    'periods' => 0, 
                    'isCont' => true
                    );

    $this->load->library('timevalue',$params);

    function index(){       

        $this->load->view('Timevalueshow_view');
        return true;
    }

}

这是库,保存在application / libraries

class Timevalue{

    private $years;
    private $rate;
    private $principle;
    private $periods;
    private $isCont;


    //zeros to test
    function Timevalue($params) {  
        $this->years = $params['years'];  
        $this->rate = $params['rate'];
        $this->principle = $params['principle'];  
        $this->periods = $params['periods']; 
        $this->isCont = $params['isCont'];
    }
    //General Getters
    function getYears(){
        return $this->years;
    }
    function getRate(){
        return $this->rate;
    }
    function getPrinciple(){
        return $this->principle;
    }
    function getPeriods(){
        return $this->periods;
    }

    //Factors
    function FVFactor(){
        if($this->isCont){
            $new_rate = $this->rate / $this->periods;
            $new_periods = $this->periods * $this->years;
            return pow(1+$new_rate,$new_periods);
        }
        else{
            return exp($this->rate*$this->years);
        }   
    }

    function PVFactor(){
        if($this->isCont){
            return pow($this->FVFactor(),-1);
        }
        else{
            return pow($this->FVFactor(),-1);
        }
    }

    //General Print
    function leprint(){
        echo "<br />Years: " . $this->years;  
        echo "<br />Rate(dec): " . $this->rate;
        echo "<br />Principle: $" . $this->principle;  
        echo "<br /># of Periods: " . $this->periods;
        echo "<br />isCont: " . ($this->isCont ? "True" : "False");
    }
}

1 个答案:

答案 0 :(得分:1)

你的控制器需要一个构造函数。它应该是这样的:

class Timevalueshow extends Controller{

    function __construct(){
        parent::__construct();
        $params = array('years' => 0, 
            'rate' => 0, 
            'principle' => 0, 
            'periods' => 0, 
            'isCont' => true
        );
        $this->load->library('timevalue',$params);
    }

    function index(){       
        $this->load->view('Timevalueshow_view');
    }
}

只有PHP5使用__construct功能。在PHP4中,它应该如下所示:

function Timevalueshow(){
    parent::Controller();
}