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

(This post was last modified: 12-28-2014, 04:04 PM by sneakyimp. Edit Reason: fixed quote blocks )

(12-27-2014, 07:57 PM)includebeer Wrote: 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.
I find it interesting that you somehow get away with only using one include/require in all of your files. I've got code related to user purchases that must include/require a lot of stuff:
* site-wide configuration for values that are not part of the framework (e.g., email credentials for SendGrid, payment gateway credentials, cloud credentials)
* Payment gateway library (e.g., paypal, authorize.net, possibly other)
* functionality to forward user to the optimal next page for purchasing behavior
* classes to get/set user access levels based on purchased digital property
* classes to create purchase-related database items for user (subscriptions, authorizations, orders)
* class to create payment journal entries for accounting
* class for error logging

I'm looking at one of my legacy files now with 13 require_once statements in it. CodeIgniter should help a lot by its clever structure and autoloading, but at least half of those includes will not go away. My legacy code has 2032 require_once statements in it. Surely we can agree that a dozen lines of autoloading code in a pre-system hook would be better? You have to admit it would result in less code redundancy. The other advantage it provides is lazy loading. The autoload function only requires the file when PHP actually encounters some usage of the corresponding class.

The errors I encountered usually happen when some useful code was used in a new context -- in a cron job, for instance. While the script that is accessed via apache might run just fine because the script handling apache requests has already included a few base classes, when I try to use the same function in a cron job, that cron job's different execution path might not have included these base classes. Failure to include/require some class often times becomes evident during testing, but in complex code, an infrequently-executed branch of code might not do it properly.  Specifically, this is the error I'm talking about:
Code:
Fatal error: Class 'Foo' not found in /var/www/erep_v2/chump.php on line 3

The solution? Manually dig around and sort out your require/include statements like a caveman. I like autoloading better.

(12-27-2014, 07:57 PM)includebeer Wrote: 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
I'm aware you can pass one parameter to your constructor using this method and I still say it's silly. Am I supposed to rewrite all of my classes such that their constructor takes only one array parameter? What is the rationale for this subversion of PHP's power by limiting my constructors to one parameter? Silly, silly, silly. Also, what about static classes that are never supposed to be instantiated?

(12-27-2014, 07:57 PM)includebeer Wrote: 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.
I'm old school in certain ways too. Personally, I think the automatic instantiation of a loaded library is a convenience I don't need and don't want -- especially if I must rewrite all my library classes to extract constructor parameters from a single parameter. I do want an autoloader. That way, I can get rid of those 2000+ require/include statements and get to the actual coding.
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-28-2014, 01:07 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