From: Camilo Sperberg on
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/
From: Camilo Sperberg on
On Wed, Jun 23, 2010 at 02:58, Rene Veerman <rene7705(a)gmail.com> wrote:

> inheritance of this kind is useful if you have common descendants for
> specific types of object
>

Nice thought. I imagine something like a DB wrapper, and implementing things
such as multiqueries and others on an extension based on the basic
connection made in the "original" class. Am I right on that?


> 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.
>

So, you're saying I can do something like this? :

class foo {
function hello($a = '') {
echo 'hello '.$a;
}
}

class bar extends foo {
function hello($a = '') {
if (!empty($a)) parent::hello($a);
else echo 'bye world';
}
}

$xx = new bar();
$xx->hello('asdf');
$xx->hello();

Which will print (hopefully, haven't test it yet):

hello asdf
bye world

That would be really a nice way of updating certain vital apps, without
breaking basic functionality.

Thanks for your answer :)

Greetings !!

--
Mailed by:
UnReAl4U - unreal4u
ICQ #: 54472056
www1: http://www.chw.net/
www2: http://unreal4u.com/