note 2411 deleted from Classes and Objects by jmoore

From: Date: Sat, 10 Feb 2001 23:32:06 +0000
Subject: note 2411 deleted from Classes and Objects by jmoore
Groups: php.notes 
Request: Send a blank email to php-notes+get-10892@lists.php.net to get a copy of this message
As far as I know the PHP Class provisions do not support executing the "parent" of overloaded functions. There is, however, a rather cumbersome work-around which can replace this missing functionality. Take the following example: <PRE> class class1{ function class1(){ echo "This is Class1"; } } class class2 extends class1{ function class2(){ echo "This is Class 2"; } } $myclass=new class2(); </PRE> In the above example, the output will say "This is Class 2" with no way to execute the "Parents" constructor. This is the same for every overloaded method, only the current one will be executed. The trick is to adopt a naming convention whereas you won't run into ambiguity and still allow the functionality to be passed to the parent function. For instance, changeing out classes to read: <PRE> class class1{ function class1(){ $this->class1_init(); } function class1_init(){ echo "This is Class1"; } } class class2 extends class1{ function class2(){ $this->class2_init(); } function class2_init(){ $this->class1_init(); echo "This is Class 2"; } } $myclass=new class2(); </PRE> This would now output both lines: <PRE> This is Class 1 This is Class 2 </PRE> This naming convention works well because every class knows two things, it knows it's own name and it knows the name of it's parent. Each _init routine would be responsible for executing it's parent _init routine. Unfortunatly, it must do this by name instead of the much cleaner "parent" method. Hopefully the "parent" method will be added soon. [Editors note (jmoore@php.net): In PHP 4.0.2+ (IIRC) you can use PARENT::class1() to call overloaded functions.]

« previous php.notes (#10892) next »