CodeIgniter Forums
Where to put plain object classes? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Where to put plain object classes? (/showthread.php?tid=420)

Pages: 1 2 3 4 5


Where to put plain object classes? - geekita - 12-02-2014

In your opinion, what's the correct place to put a plain object class file? The class I need is something similar to

PHP Code:
class PlainObject {

    private 
$id;

    public function 
getID() {
        return 
$this->id;
    }



and I want to use this class wherever I need it (a controller or a model or a helper or a library, etc). Any suggestion?


RE: Where to put plain object classes? - Rufnex - 12-02-2014

You have to put it into /application/library because its your own class.


RE: Where to put plain object classes? - dmyers - 12-02-2014

Rufnex is correct, just as a side note thou:
You would of course use $this->load->library('PlainObject'); to load this class and of course this would automatically attach it to the CI super object as a singleton. $this->plainobject->method(); etc...

If you need to create multiple "plain objects". You can then always $foo = new plainobject(); after callingĀ $this->load->library('PlainObject');
because of course $this->load->library('PlainObject'); already loaded the class.
You of course have the singleton still attached to the super object thou.


RE: Where to put plain object classes? - geekita - 12-02-2014

(12-02-2014, 06:48 AM)dmyers Wrote: Rufnex is correct, just as a side note thou:
You would of course use $this->load->library('PlainObject'); to load this class and of course this would automatically attach it to the CI super object as a singleton. $this->plainobject->method(); etc...

If you need to create multiple "plain objects". You can then always $foo = new plainobject(); after callingĀ $this->load->library('PlainObject');
because of course $this->load->library('PlainObject'); already loaded the class.
You of course have the singleton still attached to the super object thou.

Thank you all! There's an alternative way to instantiate multiple objects without calling first $this->load->library('PlainObject') ?


RE: Where to put plain object classes? - includebeer - 12-02-2014

(12-02-2014, 11:50 AM)geekita Wrote: There's an alternative way to instantiate multiple objects without calling first $this->load->library('PlainObject') ?

You can just include the file with require_once or use the loader class : http://www.codeigniter.com/user_guide/libraries/loader.html


RE: Where to put plain object classes? - Chroma - 12-02-2014

(12-02-2014, 11:50 AM)geekita Wrote:
(12-02-2014, 06:48 AM)dmyers Wrote: Rufnex is correct, just as a side note thou:
You would of course use $this->load->library('PlainObject'); to load this class and of course this would automatically attach it to the CI super object as a singleton. $this->plainobject->method(); etc...

If you need to create multiple "plain objects". You can then always $foo = new plainobject(); after callingĀ $this->load->library('PlainObject');
because of course $this->load->library('PlainObject'); already loaded the class.
You of course have the singleton still attached to the super object thou.

Thank you all! There's an alternative way to instantiate multiple objects without calling first $this->load->library('PlainObject') ?

Why would you want to do it any way other than how it was designed to work?


RE: Where to put plain object classes? - includebeer - 12-02-2014

(12-02-2014, 02:21 PM)Chroma Wrote: Why would you want to do it any way other than how it was designed to work?

Why would you create a singleton if you don't need it?


RE: Where to put plain object classes? - geekita - 12-02-2014

(12-02-2014, 02:24 PM)includebeer Wrote:
(12-02-2014, 02:21 PM)Chroma Wrote: Why would you want to do it any way other than how it was designed to work?

Why would you create a singleton if you don't need it?

That's the point!


RE: Where to put plain object classes? - ivantcholakov - 12-02-2014

But when the target class is static... What then?

I have another way by using an autoloader, based on the function spl_autoload_register(). Have a look at this project as an example: https://github.com/ivantcholakov/codeigniter-utf8

These classes I place within a separate folder application/classes/. Then, the autoloader may be declared and registered within a 'pre_system' hook: https://github.com/ivantcholakov/codeigniter-utf8/blob/master/application/hooks/autoload.php . This is just a non-standard example, you can tweak and extend the autoloader.

If the third-party class is available as a Composer package - that would be the preferred choice, of course.


RE: Where to put plain object classes? - includebeer - 12-02-2014

(12-02-2014, 03:36 PM)ivantcholakov Wrote: But when the target class is static... What then?

What's wrong with require_once ?
We don't need to reinvent the wheel just because we use a framework. Tongue