Re: class constructor overloading

From: Date: Thu, 23 Oct 2008 10:40:25 +0000
Subject: Re: class constructor overloading
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-282341@lists.php.net to get a copy of this message
2008/10/23 Alain Roger <raf.news@gmail.com>: > 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/

« previous php.general (#282341) next »