Welcome Guest, Not a member yet? Register   Sign In
A better way to OOP core libraries??
#3

[eluser]daveWid[/eluser]
There is nothing wrong with what you have, and if I just needed to add a few extra methods that is what I usually do. I want to keep the emails for each section of our site in different class files so I can just load that class to keep everything a little cleaner. So instead of having methods SectionA_create, SectionB_create, SectionC_create, I just have a class file for each section that has a create method in it. That is the beauty of OOP.

I did a little digging in the php manual and found a function that helps quite a bit, __autoload. (You can only use it if you are on php5)

So here is what I have for my NewEmail class now.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function __autoload($class)
{
    $file = 'libraries/'.str_replace('CI_', '', $class).EXT;
    if(file_exists(BASEPATH.$file)){
        require_once BASEPATH.$file;
    }
}

class NewEmail extends CI_Email
{
    public function NewEmail()
    {
        parent::CI_Email();
    
    }
    
    public function cool_method()
    {
        //Method code here
    }    
}

So now I can load in the library like normal

Code:
$this->load->library('newemail');
$this->newemail->cool_method();

The __autoload will load whatever class that php cant find. If you are on php4 or would rather just load the class you need specifically you could add this to the top of your class file. (I'll use the Email class as an example to keep in context)

Code:
require_once BASEPATH.'libraries/Email'.EXT;

Then just extend the CI_Email class like above. This way you can extend the core Email class more than once without having to get the CI instance and load in the email library.


Messages In This Thread
A better way to OOP core libraries?? - by El Forum - 04-03-2009, 10:05 AM
A better way to OOP core libraries?? - by El Forum - 04-03-2009, 11:42 AM
A better way to OOP core libraries?? - by El Forum - 04-03-2009, 12:44 PM
A better way to OOP core libraries?? - by El Forum - 04-03-2009, 01:25 PM
A better way to OOP core libraries?? - by El Forum - 04-03-2009, 03:15 PM



Theme © iAndrew 2016 - Forum software by © MyBB