Re: class constructor overloading

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2008/10/23 Alain Roger <raf.news@xxxxxxxxx>:
> 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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux