Re: [PHP] class constructor overloading
>> is it possible to overload the class construct(or) ? >> if yes, how ? No, it's not. > class A > { >function __construct() >{ >echo "A"; >} > } > > class B extends A > { >function __construct() >{ >echo "B"; >parent::__construct(); >} > } > $B = new B(); The above is an example of overriding, not overloading; PHP allows the former but not the latter. Examples of overloading include: class A { function myFunc( $boolean ) { } function myFunc( $string ) { } function myFunc( $array ) { } } Because PHP is loosely typed, the above is largely moot/unnecessary. class B { function myFunc( $boolean ) { } function myFunc( $boolean, $string ) { } function myFunc( $boolean, $string, $array ) { } } PHP allows you to get around the above by allowing you to define default values for arguments. Doing that is kind of like overloading but only in a fake-me-out kind of way. So instead of overloading the myFunc method, you could just define it as follows: class B { function myFunc( $boolean = FALSE, $string = '', $array = array()) { } } but even that doesn't ensure that the data type of each argument is as you might expect. As I stated above, PHP is loosely typed so there is nothing preventing, say, the $string argument from actually being a numeric datatype. If you are using PHP5+, you can sort of get around that by using type hinting (http://us.php.net/manual/en/language.oop5.typehinting.php) but you can only type hint objects and arrays. In closing, to answer your question, Overriding: yes Overloading: no thnx, Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] class constructor overloading
Alain Roger schreef: > thanks a lot, this is exactly what i needed. > if the construct of based class A accept arguments, i guess that construct > of class B must have the sames. > moreover, i guess that something like that must be written: I guess you find guessing preferable to RTFM and/or trying code out. > class A > { > function __construct($nameA) > { > ... > } > } > > class B extends A > { > function __construct($nameB) > { > parent::__construct($nameB); > } > } > > am i right ? are you? > > thanks. > > A. > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] class constructor overloading
2008/10/23 Alain Roger <[EMAIL PROTECTED]>: > thanks a lot, this is exactly what i needed. > if the construct of based class A accept arguments, i guess that construct > of class B must have the sames. No, you can change the signature of a method when you overload it. Below, B::__construct() accepts 1 argument while A::__construct() accepts 0 arguments: class A { function __construct() { echo "A"; } } class B extends A { function __construct( $string ) { echo $string; parent::__construct(); } } $B = new B( "B" ); If you need to force a certain signature in child classes, you can declare the parent class abstract: abstract class A { abstract function f( $a ); } class B extends A { function f( $a, $b ) // throws an error { echo "$a, $b"; } } $B = new B("B", "C"); However, if you declare the constructor as abstract, it will be ignored. -- http://www.otton.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] class constructor overloading
Alain Roger wrote: thanks a lot, this is exactly what i needed. if the construct of based class A accept arguments, i guess that construct of class B must have the sames. moreover, i guess that something like that must be written: class A { function __construct($nameA) { ... } } class B extends A { function __construct($nameB) { parent::__construct($nameB); } } am i right ? thanks. A. Yes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] class constructor overloading
thanks a lot, this is exactly what i needed. if the construct of based class A accept arguments, i guess that construct of class B must have the sames. moreover, i guess that something like that must be written: class A { function __construct($nameA) { ... } } class B extends A { function __construct($nameB) { parent::__construct($nameB); } } am i right ? thanks. A.
Re: [PHP] class constructor overloading
2008/10/23 Alain Roger <[EMAIL PROTECTED]>: > is it possible to overload the class construct(or) ? > if yes, how ? class A { function __construct() { echo "A"; } } class B extends A { function __construct() { echo "B"; parent::__construct(); } } $B = new B(); -- http://www.otton.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] class constructor overloading
Hi, is it possible to overload the class construct(or) ? if yes, how ? thx. -- Alain Windows XP SP3 PostgreSQL 8.2.4 / MS SQL server 2005 Apache 2.2.4 PHP 5.2.4 C# 2005-2008