Page 1 of 1
Just gathering opinions
Posted: Tue Jan 11, 2011 11:08 am
by joseazr1987
Well.. i was thinking about the performance of a web system and I found an small interrogant..
I use the OOP and have a class with inheritance to a data base class, and i usually I do in this way
Code: Select all
include('db.php');
class Car extends db
{
private $db;
function __construct()
{
$this->db = new db();
}
}
and my question is.. What is the best way and who used less resources to use the data base class, with inheritance or instance?
What are you think??
i think the best way is the inheritance..
because the functions that i have on the db class, are used when I call the function in the Car class and don't in the class instance
Re: Just gathering opinions
Posted: Tue Jan 11, 2011 11:51 am
by Christopher
Your example shows a class that is both inheriting a base class and also compositing an instance of that class. I am not sure why you need both? Inheritance and composition provide similar functionality. It is a long discussion of which is better in different circumstances.
Re: Just gathering opinions
Posted: Tue Jan 11, 2011 12:01 pm
by joseazr1987
Yes... I know that i use the both..
that's my question.. i just want to know your opinions about which spends less resources..
Re: Just gathering opinions
Posted: Tue Jan 11, 2011 12:53 pm
by VladSun
Best way IMHO (the "josh's way")
Code: Select all
class Car
{
protected $db;
public function __construct($db)
{
$this->db = db;
}
}
Code: Select all
$db = DB::getInstance();
$car = new Car($db);
Take a look at Singleton pattern -
http://www.php.net/manual/en/language.oop5.patterns.php
Re: Just gathering opinions
Posted: Tue Jan 11, 2011 1:06 pm
by Benjamin
Why pass dependencies as arguments? The class can pull it's own dependencies which is cleaner because you're not creating instances of class(es) every time you call it. Either way, if you leave out a dependency the class won't work.
Re: Just gathering opinions
Posted: Tue Jan 11, 2011 1:07 pm
by joseazr1987
Thanks VladSun
I appreciate your opinion.. I just still Newbie in this of php, i had seen examples for patterns..
but really i can't understanding well.. I'll still studing the theme.
but thank's for the help
Re: Just gathering opinions
Posted: Tue Jan 11, 2011 2:02 pm
by Christopher
Benjamin wrote:Why pass dependencies as arguments? The class can pull it's own dependencies which is cleaner because you're not creating instances of class(es) every time you call it. Either way, if you leave out a dependency the class won't work.
If you pass it you are creating a dependency on an interface -- not a class. Better for testing, refactoring, polymorphism, etc.
Re: Just gathering opinions
Posted: Tue Jan 11, 2011 9:02 pm
by josh
Benjamin wrote:The class can pull it's own dependencies
Same reason a class should not pull data from a global variable. Causes various code smells.
http://googletesting.blogspot.com/2008/ ... oject.html
Not only does dependency injection let you mock out the object, it lets you implement the strategy pattern. For example if every class is hard coded to a specific strategy, adding a second one would involve editing much duplicated code. Its a myth that dependency injection makes your API hard to use, you do not in practice need to pass "every" object to "every" other class. For example A car object may only get passed an Engine & Chassis object, and database object. The database object would not have to be passed to the engine object & chassis objects manually, simply passing it to the car object would cause that to happen. The important part is that if you
wanted to pass a different adapter to the engine, you could.
The reasons to do it one way VS the other, joeseazr1987, is not for performance but rather to keep the code clean. Things that are more likely to cause performance slow downs is doing inefficient operations during loops, things like that. The way you structure your code however dictates how easy it will be to develop & test within that code base. If its hard to develop, performance will be hard to get. If its easy to develop, performance will come easy.
Re: Just gathering opinions
Posted: Wed Jan 12, 2011 12:54 pm
by Benjamin
That makes sense. The article you posted a link to doesn't really help though because I don't think a lot of the problems they run into would actually ever happen in PHP. For example the article is saying that tests were failing because portions of the code hadn't yet been initialized. In PHP though, the objects would have initialized the dependencies themselves.
Re: Just gathering opinions
Posted: Wed Jan 12, 2011 2:07 pm
by VladSun
Honestly, while reading the patterns theory I got the impression that one of the most important reasons to use them is to get as less as possible sources (i.e. class source files) to be recompiled. I don't mean that it is the one and only reason to use some of the patterns, but I do think that PHP (and other "scripting" languages) can use some more (or less probably) elegant forms of the patterns. It's even mentioned several times in the "Design patterns" book.
Recently, I've been thinking about posting a topic about this - how PHP design patterns implementations differ from C/C++/Java etc. implementations.
Re: Just gathering opinions
Posted: Thu Jan 13, 2011 4:50 am
by josh
Benjamin wrote:That makes sense. The article you posted a link to doesn't really help though because I don't think a lot of the problems they run into would actually ever happen in PHP. For example the article is saying that tests were failing because portions of the code hadn't yet been initialized. In PHP though, the objects would have initialized the dependencies themselves.
In the example initialization was a method in userland, I think you were a little confused, It is very much so applicable to PHP, and every other object oriented language in existence. If code is hard to test, its by definition hard to re-use your code in another context. Keep that in mind & check out this video. I agree that article isn't convincing on it's own, but is more illustrative than I could be. However this video is what really did it for me. Try to bear with his accent, the talk is brilliant.
http://www.youtube.com/watch?v=-FRm3VPhseI I had to watch it multiple times before it all sunk in personally. He users the car analogy which will hit home with you given your current project. I know it did for me, being in the automotive software industry myself.
Re: Just gathering opinions
Posted: Thu Jan 13, 2011 7:28 am
by Jenk
In my opinon, the entire point of using objects is to break the dependency chain. If an object is initialising it's own dependencies, you've merely moved your procedural code into classes.
Something such as this:
Code: Select all
class Foo
{
private $db;
public function __construct()
{
$this->db = new Database();
}
}
breaks that very rule for two reasons - it is impossible to use Foo without the implementation of "Database" - and it is impossible to
see that dependency without looking at Foo's implementation.
Where as:
Code: Select all
class Foo
{
public function __construct($db)
{
$this->db = $db;
}
}
Means Foo has a dependency on the Interface of $db, not the
implementation of $db, and it is
visible to anyone using Foo without needing to view the implementation.
edit: What gives with the parenthesis escaping?
Re: Just gathering opinions
Posted: Thu Jan 13, 2011 1:56 pm
by josh
Jenk wrote:edit: What gives with the parenthesis escaping?
viewtopic.php?f=7&t=117638
you must use
Code: Select all
due to the fact forwards compatibility on [php] tags was not maintained over time by the forum administrators. When I quote your post the code looks good, so in theory they should replace "[php]" with "[syntax=php]" with a rewrite rule