CodeIgniter V3

#11
[eluser]ColonelChlorine[/eluser]
Hey Phil, as an idea ... why not provide a boolean parameter to the load method which declares whether or not to instantiate the class?

This way the file will be required (once), but the class not instantiated ... thereby getting rid of some of the singleton problems.

For example I would make a model called, "Company" which doesn't extend CI_Model, but has it's own internal access to CI via =& get_instance(). That way I could do something like:

Code:
$this->load->model('Company', false);
$company1 = new Company();
$company2 = new Company();
$company1->add_partner($company2);
$company1->save();

Does that make sense?

#12
[eluser]Phil Sturgeon[/eluser]
That would break the current 2nd parameter. A 3rd param could be ok, but a little ugly.

#13
[eluser]ColonelChlorine[/eluser]
Hmm agreed that is quite nasty. In my project I might extend CI_Loader and add a new method ... something like, "class". This would do most of the same stuff as model but not instantiate.

#14
[eluser]WanWizard[/eluser]
@ColonelChlorine,

As long as the files are in the locations CI expects them, using $this->load is not an issue. Even if it creates the 'singleton' (which technically it isn't), nobody is stopping you from using "new" once CI has loaded it.

And if the presence of the $this->property bothers you, unset if after load...

#15
[eluser]ColonelChlorine[/eluser]
Interesting ... my only response is that it probably interferes with constructors of my classes. You're also right, it isn't a true singleton, I am referring mainly to the automatic class initialization. Nothing is preventing me from making more class-babies (really should be called that rather than objects).

That said, if I'm just making new instances later, I can just ignore that fact as long as the constructor doesn't affect other parts of the application.

#16
[eluser]WanWizard[/eluser]
A constructor should be used for the construction process of the object (a specific class instance).
It should not matter from where and how many times it is instantiated.

#17
[eluser]ColonelChlorine[/eluser]
Just as a note in that implementation: your class's constructor cannot have mandatory parameters. You will get PHP warnings because CI does not provide those parameters. The way I am getting around this is to provide defaults. It seems to work fine after that.

Also I have a technical question: will creating a large number of instances of this class result in a massive increase in memory usage if the class extends CI_Model? Currently mine don't, and simply refer to get_instance() by reference internally. I'm just wondering if extending a large parent class repeatedly will affect memory/performance.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.