Welcome Guest, Not a member yet? Register   Sign In
Developing Codeigniter Libraries with proper Codeigniter / OOP fundamentals
#1

[eluser]TheresonAntaltego[/eluser]
Greetings!

I must thank CI, its amazing documentation, and the video tutorials for helping me complete a jump to more OO thinking. Most OOP examples and tutorials fail to connect with me in a sensical way. I saw how CI relates URI segments to PHP objects and their methods, and OOP clicked for me. Now I get it. I realize PHP 4 and CI may not present the whole picture of OOP programming fundamentals, but I've come a long way. And, beyond the basic hello world introduction, I've jumped right into developing a few codeigniter libraries.

So, I am a little confused as to how to get libraries to talk to each other. Say I have three libraries, one each for XML parsing, making CURL requests, and interfacing to some external API. If the proper way to load each library in CI is as follows:

Code:
... controller ..

... constructor() {

parent::constructor();
$this->load->library('the_xml_library');
$this->load->library('the_curl_library');
$this->load->library('the_external_API_library');
}

I can only see two ways for each to reference each other. The first, would be to use get_instance within the library with dependencies (the external API), and reference the required library by name in the CI super object. In this case, if I wanted to drop in a replacement library for either XML or CURL handling, I'd have to tamper with the API library. That doesn't seem right with the concept of agnostic modularity in program design. eh?

The second, would be to either pass in a reference to an instantiated dependency upon instantiation of the dependant library, or have methods within the libraries used to set references to each other. This also seems a little goofy to me.

Theoretically, each of these libraries should be PHP classes which should (or could) function without CI at all, and/or dropped in and replaced at will if they _are_ operating within CI.

I am missing something. And its either an understanding of how to develop a library for CI, or an understanding of simple OOP principles that would make my libraries pluggable. Class? Bueller?

Thank you so much for your expert tutelage.
#2

[eluser]bradym[/eluser]
Hey Tyler,

Welcome to CodeIgniter. Smile

It's been my experience that code is greatly simplified and much easier to maintain when avoiding cross-class interaction. Instead of calling the xml class from within the curl library, perform your xml magic and then pass the results into the curl library (or vice versa).

Code:
// Do your xml magic here
$xml = new xml_library($data);

// Pass the results to curl
$curl = new curl_library($xml);

Of course, it may be more logical to do something like this:

Code:
$xml = new xml_library($data);

$curl = new curl_library();
$curl->set_xml($xml);

Does this help any?

Brady
#3

[eluser]TheresonAntaltego[/eluser]
Well Brady, It gets the conversation going... so yeah! it helps. :-)

I suppose the first thing to address might be the proper coding of the classes to allow for instantiation of those objects (CI libraries) in any order. In one case, say, retrieving info, the API calls need to be known and defined before CURL requests are made and any XML is received. In which case these are significant in the following order
1) API
2) CURL
3) XML
4) API post-processing of XML

And in another, sending info, XML needs to be generated and API calls known before CURL is brought into action. Where our order of importance is
1) XML
2) API
3) CURL

I think that i just may not yet possess the knowledge how to code my classes to not be dependent upon any order of instantiation? yes, that is a question. :-)
#4

[eluser]wiredesignz[/eluser]
If your libraries are co-dependent then you should use require or require_once prior to the class definition and then instantiate the required object(s) inside your class.

CI itself relies on get_instance() in many libraries to reference the core (controller) and while this works within the CI structure, it does not follow good practice, usually a reference to the core would be passed into the library as required.




Theme © iAndrew 2016 - Forum software by © MyBB