Re: [PHP] In what scenario an extension of a class is useful?

2010-06-29 Thread Camilo Sperberg
On Wed, Jun 23, 2010 at 02:58, Rene Veerman rene7...@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/


Re: [PHP] In what scenario an extension of a class is useful?

2010-06-23 Thread Rene Veerman
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



RE: [PHP] In what scenario an extension of a class is useful?

2010-06-23 Thread Daevid Vincent
Priceless. Could also be extended applied to babies too. ;-p

object babies extends mammal (
  function eat (milk $food)
 )


ROFL.

 -Original Message-
 From: Rene Veerman [mailto:rene7...@gmail.com] 
 Sent: Tuesday, June 22, 2010 11:58 PM
 
 object mammal (
  function eat (mixed $food)
  function shit ()
  function sleep()
 )


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php