如何扩展自己的库?

时间:2015-07-06 15:12:31

标签: codeigniter libraries extend

我是codeigniter的新手,想要在我的第一个扩展另一个库的ci应用程序中添加一个库

class Mylib {}

/application/libraries/Mynewlib.php:

class Mynewlib extends Mylib{}

我在哪里放置Mylib.php以及如何加载Mylib?

谢谢!

4 个答案:

答案 0 :(得分:4)

在这种情况下,我仍然在寻找最好的实践。

在我的情况下,我有两个类应该从父类扩展。 (付款 - 家长班; Payment_paypal - 与Paypal互动; Payment_nganluong - 与我国内网关Ngan Luong互动 对于每个支付网关,我必须编写一些属性,方法来处理,但几乎基本属性和主题方法是相同的。

我的解决方案:我创建了4个文件:

  1. Payment_base.php

    class Payment_base { // base properties and method }

  2. Payment.php

    require_once (APPPATH.'/libraries/Payment_base.php'); // this is an instance of payment_base, // when you want to use base method and properties // just call $payment->... // class Payment extends Payment_base { public function __construct(){ parent::__construct() } }

  3. Payment_paypal.php

    require_once (APPPATH.'/libraries/Payment_base.php'); class Payment_paypal extends Payment_base{} ////////////////////////////////////////// require_one 'Payment_base.php'; class Payment_paypal extends Payment_base { // properties and method }

  4. 4,Payment_nganluong.php

    require_once (APPPATH.'/libraries/Payment_base.php');
    class Payment_nganluong extends Payment_base { 
    // properties and method
    }
    

    那是它,在控制器中:

    $this->load->library('payment');
    $this->load->library('payment_paypal');
    $this->load->library('payment_nganluong');
    $this->payment->myMethod(); // base method
    $this->payment_paypal->charge(); // call to paypal to charge money
    $this->payment_nganluong->checkout(); // check out with Ngan Luong
    // no need to load and use payment_base
    

    希望对你有所帮助。

答案 1 :(得分:2)

这适合我。

库文件Mylib.php

class Mylib {
   function test() 
   {
       return 1;
   }
}

库文件Mynewlib

require_once('Mylib.php');   
class Mynewlib extends Mylib{}

控制器

$this->load->library('Mynewlib');
echo $this->Mynewlib->test();

答案 2 :(得分:0)

您在application/libraries/mylib.php

中创建了一个文件
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MyLib
 {

    function __construct() 
    {
         // initialize variables if you want
    }

    public function  my_function($something)
    {
    $out = $something . " World";
    return $out;
    }

}

/* End of file mylib.php */

然后在您的控制器中,您可以像这样调用您的库

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class my_controller extends CI_Controller 
{
    public function index()
    {
        $this->load->library('mylib'); // call the custom class MyLib
        $term = $this->mylib->my_function("Hello");
        echo $term; // Hello World
    }
}
/* End of file my_controller.php */
/* Location: ./application/controllers/my_controller.php */

希望有所帮助

有关详细信息,请参阅http://www.codeigniter.com/userguide2/general/creating_libraries.html

答案 3 :(得分:0)

您可以在库中使用get实例来加载

中的另一个库
<?php

class MyLib {

public function __construct() {
    $this->CI =& get_instance();
    $this->CI->load->library('mylib2');

}

public function something() {
   $this->CI->mylib2->some_function();
}

}