Where to put plain object classes? |
12-02-2014, 05:17 PM
(This post was last modified: 12-02-2014, 05:19 PM by ivantcholakov. Edit Reason: Address to a person )
@includebeer
Something is wrong with me, require_once is OK. I am lazy. Off-topic: Future without $this->load->this() and $this->load->that() would be perfect for me. :-) (12-02-2014, 04:35 PM)includebeer Wrote:(12-02-2014, 03:36 PM)ivantcholakov Wrote: But when the target class is static... What then? Unfortunately, require_once doesn't take in account CodeIgniter Packages. https://ellislab.com/codeigniter/user-gu...oader.html
(12-08-2014, 06:07 PM)dmyers Wrote: Unfortunately, require_once doesn't take in account CodeIgniter Packages. The original question was about a simple class...???
Yet another example about autoloading classes by using a hook, this time for supporting base controllers:
http://avenir.ro/codeigniter-tutorials/n...ontroller/ (12-02-2014, 06:48 AM)dmyers Wrote: Rufnex is correct, just as a side note 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?
(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: 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.
Using Composer is the way to go. But you don't have to setup a package with packagist or any of that stuff. Just use if for it's autoloader.
You can make a new folder anywhere you want. Call it "mystuff". Now, create a composer.json file with the following contents: Code: { Then you can create classes with the MyStuff namespace and have Composer provide the autoloading.
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 reject your reality and substitute my own" - Adam Savage, M5 Inc.
(12-20-2014, 05:53 PM)Hobbes Wrote: easiest way: 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); 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.
(12-27-2014, 05:30 PM)sneakyimp Wrote: * 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. Code redundancy and bugs? It's only one line of code. I can live with one redundant line! I'm curious what kind of bugs you had with include/require you don't have with an autoloader. (12-27-2014, 05:30 PM)sneakyimp Wrote: * 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. You can pass arguments to the loader class in the second parameter: $params (array) – Optional array of parameters to pass to the loaded library’s constructor http://www.codeigniter.com/userguide3/li...-reference (12-27-2014, 05:30 PM)sneakyimp Wrote: * What if I need multiple instances of MyClass to perform some operation. Then use require_once! Maybe I'm too old school, but I don't understand why people have trouble doing simple things just because they use a framework. What would you do in plain PHP? If CI doesn't provide what you're trying to do then do it in plain PHP. |
Welcome Guest, Not a member yet? Register Sign In |