Accessing class instances
Posted: Sat May 31, 2008 11:55 pm
This is split from: viewtopic.php?f=19&t=83441 so we don't hijack onions thread.
I can see how you would like that better, however I would rather type $this->kernel->blah than create tons of individual functions for every class or common task. I'm a very fast typer so it really doesn't bother me too much. Besides, it's less typing that having to create new instances of each class all over the place.Mordred wrote:So, basically you've invented global functions with lots of -> thrown in.astions wrote:Yeah either way. You just need to get an instance of it over the other class somehow. In my framework I use a kernel that in addition to a number of other things, holds an object of every class. So I can call any method of any class from any place and it saves a ton of memory PLUS it's very quick.
I'm all for the global functions to access "kernel" functionality, but I hate typing useless syntax sugar around, just because PHP doesn't have any reasonable scope modifiers (and no, 'global' is not reasonable).
For example instead of $this->kernel->db->query("blah") I do DBQuery("blah"), you do the math on how much typing it saves and how much readability it adds. I do have a DB object that I can pass around should I ever want to use two DB connections simultaneously, but I've yet to come to this need. Instead, with this wrapper of the "default" db connection, I cover 99.999% of use cases with significantly less typing and maintenance code. I would not bear to type a hundred -> which will always have the same object on the left.