Accessing class instances

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Accessing class instances

Post by Benjamin »

superdezign wrote:Using include() for the necessary classes is considered better practice than the autoload capability created by PHP.
How come?
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Accessing class instances

Post by Mordred »

superdezign wrote:But in the concepts that OOP was built on, objects are created and the only objects with access to them are the ones that need access. It's the whole issue of "uses" and "has." If an object only outputs HTML, and it doesn't use the database to do that, then the object should not have access. Also, the objects that do have access won't be easily identified from the objects that don't, as any object can simply add that call.
We're ballancing here between the purity of abstract design and the practical process of developing code in one particular language. Noone would incidentally call a database function, so that we need to think about OOP protection. Especially considering that PHP won't warn us at compile time. But if I want to call a database function, I want to be able to do it easily, short and to the point. For the folks that want strict encapsulation and mocked up unit testing (can't imagine why in this case), I still support the $this->pDatabase->Query() style.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Accessing class instances

Post by superdezign »

astions wrote:
superdezign wrote:Using include() for the necessary classes is considered better practice than the autoload capability created by PHP.
How come?
In order to clearly see which classes are using which classes. Especially useful when changing code and you accidentally leave in a class that is no longer necessary (as PHP will notify you that the class cannot be found), and when looking directly in an included class as opposed to creating documentation for it.
Mordred wrote:But if I want to call a database function, I want to be able to do it easily, short and to the point.
But in doing so, wouldn't you be giving more database responsibilities to classes that potentially should have very little? For example, result-handling and error-handling.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Accessing class instances

Post by Mordred »

superdezign wrote:
Mordred wrote:But if I want to call a database function, I want to be able to do it easily, short and to the point.
But in doing so, wouldn't you be giving more database responsibilities to classes that potentially should have very little? For example, result-handling and error-handling.
No. If I wanted to call the database, I would have called it. We're not talking about responsibilities here. What we discuss here is how do we access the database object when we know we want to. Or the response object. Or the error logging object.
My argument is that for some of these, you would do better with a global function to work around the PHP's limitations. Read the start of the thread, I don't want to repeat myself (and others).
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Accessing class instances

Post by superdezign »

Finding any other arguments against you is impossible then. :P

Basically, you and ~astions are doing the same thing, but your requires less typing and his looks more like OO code than procedural. Your's is still OO, but the call is to a global function rather than a global singleton or a global registry. It just seems to be a matter of style, at this point.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Accessing class instances

Post by Benjamin »

superdezign wrote:In order to clearly see which classes are using which classes.
Why is that good? What if every single class could use any other class? That's how my framework works.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Accessing class instances

Post by superdezign »

astions wrote:Why is that good? What if every single class could use any other class? That's how my framework works.
Well technically, every class could always use any other class. The usage isn't a matter of design or overhead, but a matter of the fact that we make mistakes sometimes. Being able to account for everything that is being used without searching for every constructor and static method call in the class can simplify our debugging.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Accessing class instances

Post by Benjamin »

What would the mistake be?

For example if I've got a collection of classes that have all been tested and verified to return the correct results, and an error was occurring, how could that possibly happen?

In my framework the only way that could happen is at the point of entry, such as post data. BOOM, the problem is in the validation code.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Accessing class instances

Post by superdezign »

It helps to stop errors in the creation of the code, not so much run-time errors.

If you mistype a class name (which happens, especially if you happen to be hungry or distracted :P) in the code, but your code autoloads classes, then you have one less check than you could if you had included the file explicitly. Thus, you could use the wrong class, and, if they are similar in structure, not notice for a variable amount of time.
Post Reply