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

(12-20-2014, 05:53 PM)Hobbes Wrote: easiest way:

put it into the application/libraries folder
autoload this library in your application/config/autoload.php

then from anywhere in your controllers/models user: $this->library_name_here->some_function();

from other libraries in application/libraries or from helper files: $ci = get_instance(); $ci->library_name_here->some_function();

I really must contest that this is the 'easiest' way. This approach seems totally silly to me for the reasons I pointed out above:
* What if a class is only to be used statically. I.e., you NEVER instantiate this class for any reason because all of its methods are static.
* What if you must supply arguments to the constructor for the class? This would in all likelihood cause a fatal error if CodeIgniter tried to instantiate the object without providing these parameters.
* What if I need multiple instances of MyClass to perform some operation. E.g.:
Code:
$player1 = new Player("Mutt", $foo);
$player2 = new Player("Jeff, $bar);
In that case, why on earth would I tolerate the overhead of CI trying to instantiate the object??

ivantcholakov seems to have the most helpful suggestions in this matter. I agree that an autoloader is extremely useful for eliminating thickets of require/include statements. It's been my experience that these include/require statements are the source of much code redundancy and many bugs.

As for using Composer, I am not familiar with it, but that seems to me to introduce a completely unnecessary dependency and additionally requires one to maintain composer.json as well each time one adds additional classes or libraries. Perhaps I'm missing something.
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 sneakyimp - 12-27-2014, 05:30 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