Reputation: 172
I am new in codeigniter and want to add a library to my first ci app which extends another library
class Mylib {}
/application/libraries/Mynewlib.php:
class Mynewlib extends Mylib{}
Where do I have to put Mylib.php and how do I load Mylib?
Thanks!
Upvotes: 2
Views: 7565
Reputation: 328
This works for me.
library file Mylib.php
class Mylib {
function test()
{
return 1;
}
}
library file Mynewlib
require_once('Mylib.php');
class Mynewlib extends Mylib{}
controller
$this->load->library('Mynewlib');
echo $this->Mynewlib->test();
Upvotes: 2
Reputation: 167
I'm still looking for best pratice in this case.
In my case, I have 2 classes should extend from a parent. (Payment - parent class; Payment_paypal - interact with Paypal; Payment_nganluong - interact with Ngan Luong, my domestic gateway) With each payment gateway, I have to write some properties, method to process but almost base properties and method of theme are the same.
My solution: I created 4 file:
Payment_base.php
class Payment_base {
// base properties and method
}
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()
}
}
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, Payment_nganluong.php
require_once (APPPATH.'/libraries/Payment_base.php');
class Payment_nganluong extends Payment_base {
// properties and method
}
That's it, and in controller:
$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
Hope some helpful for you.
Upvotes: 5
Reputation:
You could use get instance in library to load another library in
<?php
class MyLib {
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('mylib2');
}
public function something() {
$this->CI->mylib2->some_function();
}
}
Upvotes: 0
Reputation: 2876
you create a file in 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 */
then in your controller you call your library like this
<?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 */
Hope that helps
see http://www.codeigniter.com/userguide2/general/creating_libraries.html for more details
Upvotes: 0