inheritance of this kind is useful if you have common descendants for
specific types of object
object mammal (
function eat (mixed $food)
function shit ()
function sleep()
)
object carnivore extends mammal (
function eat (meat $food)
)
object herbivore extends mammal (
function eat (veggies $food)
)
now you can call the descendant mammal-eat() from carnivore-eat()
and herbivore-eat() to process foods.
their mammal-vitality settings (lets say its a subproperties-array)
will be updated, with custom handling taking place in carnivore-eat()
and herbivore-eat() while mammal-eat() is likely to update those
vitality properties..
another use, is a modification of a certain component.
descend it from the parent object and override the functions.
you can do pre processing and post processing by overriding functions.
On Wed, Jun 23, 2010 at 5:58 AM, Camilo Sperberg unrea...@gmail.com wrote:
Hello everybody :)
I'm really intrigued on something... In what real-world applications could
an extension of a class be really useful?
Let's say I have this code:
class foo {
function hello_world($a) {
echo 'foo hello world';
}
function bye_world() {
echo 'foo bye world';
}
}
class bar extends foo {
function hello_world($a,$b) {
echo 'bar hello world';
}
}
Point 1: Why not just overwrite the hello_world method in the foo class in
the first place? Wouldn't that save code and possible incompatibility or
consistency issues between the code you've already written and between the
two classes ? (Assuming that you do some things based on the $a and $b
values).
Point 2: On the other hand, maybe I could apply different operations to both
(e.g.: return 1 in foo and 2 in bar), without breaking the basic
functionality already achieved in the foo class. (Maybe considering that I
want to apply an update or patch to an already existing application,
however, is this is the scenario, I should always fix the old code wherever
I invoke the foo class which returns us to point 1).
Point 3: Ok, maybe I don't want a specific class to be so huge and I
separate it into pieces of classes. But then again, wouldn't it be simpler
to just save some code and keeping only one file with the entire class?
Is it just that or do I miss something else? I'm not saying it is useless,
it sounds indeed fantastic to work with... but I just can't imagine in what
real-world cases this would be useful.
Greetings !
--
Mailed by:
UnReAl4U - unreal4u
ICQ #: 54472056
www1: http://www.chw.net/
www2: http://unreal4u.com/
--
-
Greetings from Rene7705,
My free open source webcomponents:
http://code.google.com/u/rene7705/
http://mediabeez.ws/downloads (and demos)
My music (i'm DJ firesnake)
http://mediabeez.ws/music
http://www.facebook.com/rene7705
-
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php