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

[eluser]daveWid[/eluser]
I want to extend the Email class, but since I have different sections of the website, I want to have different classes to handle the emails being sent out to keep everything clean.

Here is what I have

application/MY_Email.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Email extends CI_Email
{
    public function MY_Email()
    {
        parent::CI_Email();
    }
}

class NewEmail extends MY_Email
{
    public function NewEmail()
    {
        parent::MY_Email();
    }

    public function cool_method($params)
    {
        // Method code here
    }

}

And in my controller where I want to send the email I have this

Code:
$this->load->library('email');
$email = new NewEmail();
$email->cool_method($params);

All of this works great, but I was wondering if there was a way to load up the NewEmail class directly. Is there a way to call

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

and have it autoload the CI_Email class?? I want to extend the CI_Email class without holding a reference to the base CI class with get_instance().

Thanks for any help in advance.
#2

[eluser]TheFuzzy0ne[/eluser]
Why do you need a new instance of the Email class?

$this->load->library('email'); loads the Email class so you can access it through $this->email. To auto load the class, you can either put that line of code into your controller constructor, or add it to your autoload.php file, in the right section.

The problem as I see it, is that you're declaring both of those classes in the same file, when perhaps you shouldn't be.

What's wrong with this(?):
./system/application/libraries/MY_Email.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Email extends CI_Email
{
    public function MY_Email()
    {
        parent::CI_Email();
    }

    public function cool_method($params)
    {
        // Method code here
    }

}
#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.
#4

[eluser]TheFuzzy0ne[/eluser]
What would cool_method() be, exactly? I can't help thinking that what you really need is a configuration for each part of the site, not a different library. The settings can be loaded from a model, and voila!
#5

[eluser]daveWid[/eluser]
Well what I needed this for is emails so methods would send account creation emails, forgot password emails and things like that. Our site has 3 different styles of users and there are a few different languages that are used.

Maybe the way I described above is overkill but it works well for me. I guess that is the beauty of CI, it is very flexible so you can mold it in your own way.

Anyway you could give me an example of how to set up a configuration for each part of the site? Not really sure what you mean, but if can improve workflow I'm always open!




Theme © iAndrew 2016 - Forum software by © MyBB