Welcome Guest, Not a member yet? Register   Sign In
Where to put plain object classes?
#16

(12-17-2014, 01:15 PM)sneakyimp 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.

Am I to understand that simply loading a class definition results in a controller attempting to instantiate the object? That seems strange to me. What if the object requires arguments to a constructor? What if you don't want a property attached to the controller? Also, what if the object is in fact an abstraction that serves as an ActiveRecord interface for a database? Might such classes belong in the model directory?

There is no an "official" way of doing that. The manual loader comes from PHP4 times and it stays as it is.

You have two options for using framework-agnostic classes:

1. To make a custom autoloader. I proposed (and I recommend) it to be activated by using a hook, because it seems to me less "hackish" way. In my developments I modify the core files anyway for various reasons, this is why I include the autoloader at some point of bootstrapping. Yes, autoloading models is nice, because I can extend them without the need for include/require. See a sample rich implementation: https://github.com/ivantcholakov/starter...toload.php

I prefer this option just to put my stuff and to use it without any bureaucracy.

2. The second option is Composer. I prefer it only for ready yet external components. During my work I don't want to write packages of my own (and to upload them to Packagist? - no way, no time for that). I just want to get the job done.
Reply


Messages In This Thread
Where to put plain object classes? - by geekita - 12-02-2014, 05:51 AM
RE: Where to put plain object classes? - by ivantcholakov - 12-17-2014, 04:35 PM
RE: Where to put plain object classes? - by Jamie - 01-06-2015, 05:18 AM
RE: Where to put plain object classes? - by Jamie - 01-06-2015, 03:08 PM



Theme © iAndrew 2016 - Forum software by © MyBB