How come?superdezign wrote:Using include() for the necessary classes is considered better practice than the autoload capability created by PHP.
Accessing class instances
Moderator: General Moderators
Re: Accessing class instances
Re: Accessing class instances
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.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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Accessing class instances
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.astions wrote:How come?superdezign wrote:Using include() for the necessary classes is considered better practice than the autoload capability created by PHP.
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.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.
Re: Accessing class instances
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.superdezign wrote: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.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.
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).
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Accessing class instances
Finding any other arguments against you is impossible then. 
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.
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.
Re: Accessing class instances
Why is that good? What if every single class could use any other class? That's how my framework works.superdezign wrote:In order to clearly see which classes are using which classes.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Accessing class instances
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.astions wrote:Why is that good? What if every single class could use any other class? That's how my framework works.
Re: Accessing class instances
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.
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Accessing class instances
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
) 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.
If you mistype a class name (which happens, especially if you happen to be hungry or distracted