In PHP the programmer has to call the constructor of the super class.
The derived class has to know the name of his "super"(java syntax). 
In PHP5 the constructor will have unified name, there will not be a need the derived 
class to know super class name.
So
<?php

class Base{
var $base_var="empty";
    function Base($init){ print $init." in BASE ";$this->base_var = $init;}
}
class Extended extends Base{
var $ext_var;
    function Extended($init){
        $this->Base($init.$init);
        print "\n".$init." in Extended";
        $this->ext_var =$init;
    }
}
$a = new Extended("FUBAR");
print "\n";
var_dump($a);

?>
FUBAR in BASE 
FUBAR in Extended
object(extended)(2) {
  ["base_var"]=>
  string(5) "FUBARFUBAR"
  ["ext_var"]=>
  string(5) "FUBAR"
}

<?php

class Base{
var $base_var="empty";
    function Base($init){ print $init." in BASE ";$this->base_var = $init;}
}
class Extended extends Base{
var $ext_var;
    function Extended($init){
//        $this->Base($init.$init);
        print "\n".$init." in Extended";
        $this->ext_var =$init;
    }
}
$a = new Extended("FUBAR");
print "\n";
var_dump($a);

?>

FUBAR in Extended
object(extended)(2) {
  ["base_var"]=>
  string(5) "empty"
  ["ext_var"]=>
  string(5) "FUBAR"
}


----- Original Message ----- 
From: "Erik Price" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, April 11, 2002 9:04 PM
Subject: [PHP] constructors in derived classes


> I have another question about OO in PHP --
> 
> The manual (http://www.php.net/manual/en/language.oop.constructor.php) 
> does an excellent job of explaining how constructors in extended classes 
> work when there are constructors in base classes.  I understand that no 
> problem.  But at the bottom of the page is a cautionary note:
> 
> <cautionary_note>
> Neither PHP 3 nor PHP 4 call constructors of the base class 
> automatically from a constructor of a derived class. It is your 
> responsibility to propagate the call to constructors upstream where 
> appropriate.
> </cautionary_note>
> 
> This is where I am uncertain -- how do I call an upstream constructor 
> from an instance of an extended class, where I have a constructor in the 
> extended class?  IOW:
> 
> <?php
> class ClassA
> {
> function ClassA($var)
> {
> print "ClassA object instantiated, argument is $var";
> }
> }
> 
> class ClassB
> {
> function ClassB($var)
> {
> print "ClassB object instantiated, argument is $var";
> }
> }
> 
> $instance = new ClassB(frank);
> 
> // this should print "ClassB object instantiated, argument is frank"
> 
> My question is, how do I call the constructor of ClassA?  To see the 
> following results:
> 
> // "ClassB object instantiated, argument is frank"
> // "ClassA object instantiated, argument is bob"
> 
> 
> 
> 
> Erik
> 
> 
> 
> 
> ----
> 
> Erik Price
> Web Developer Temp
> Media Lab, H.H. Brown
> [EMAIL PROTECTED]
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 


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

Reply via email to