Re: [PHP] constructors in derived classes

2002-04-11 Thread Erik Price


On Thursday, April 11, 2002, at 02:17  PM, Andrey Hristov wrote:

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

Andrey,

Thank you for the detailed explanation.  So to sum it up, you need to 
call the base class's constructor from within the extended class's 
constructor if you want the effect of both constructors happening at 
once, as the extended class is instantiated into an object.

I've noticed that it -is- possible to call the base class's constructor 
from the script, like this:

// classes are Base and Extended
$instance($arg1) = new Extended;

// directly access the base class's constructor
$instance->Base($arg2);

and it seems to work.  But this is really just shortchanging yourself 
the value of having a constructor in Base, since you are calling it as a 
method and not using its "automaticness" feature.

Thanks again,


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




Re: [PHP] constructors in derived classes

2002-04-11 Thread Andrey Hristov

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
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"
}

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




[PHP] constructors in derived classes

2002-04-11 Thread Erik Price

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:


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.


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:

http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php