PHP is has a strong community, and by that I mean there are a lot of good programmers out there developing opensource classes for PHP.
Personally, Codeigniter is my favorite PHP framework. It’s very fast and has a lot for built-in libraries that helps you code faster. But in the field of creating libraries, you can’t just copy and paste any existing classes as a CI library. There are certain requirements to be met. Fortunately there is a simple way to make almost any class CI friendly, all you have to do is use the power of OOP.
Integrating PHP classes as Codeigniter library
The thing with CI libraries is that they you have to pass only 1 parameter to the constructor as an array. The idea here is to extend the PHP library and modify its constructor so that it takes only 1 array as a parameter.
Let’s take a look at the library above. mycustomclass.php is a PHP class and samplelib.php is the library file. The custom class takes 3 parameters, but by creating a subclass, I am going to combine the parameters into 1 array.
Inside the samplelib.php it looks like this:
<?php require 'mycustomclass.php'; class Samplelib extends MyCustomClass{ function __construct($params = null){ parent::__construct($params['parameter1'],$params['parameter2'],$params['parameter3']); } } |
Below is an example on how to use the library
<?php $this->load->library('samplelib', array( 'parameter1' => 'foo', 'parameter2' => 'bar', 'parameter3' => 'name', )); $this->samplelib->someFunction(); |