PHP 5 abstract method and class type hints of extending classes

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

 



The quesion is: how would one make an abstract method that can be compatible 
with all extending classes that define the method using different class type 
hints?

The php block below is how I thought it should work, but will give this 
error at parse time:
<error>Fatal error: Declaration of DisplayObjectOne::display() must be 
compatible with that of DisplayBase::display()</error>
because the hint in the concrete method is different from that in the 
abstract method.

<?php
// base display class
abstract class DisplayBase {
    abstract public function display(ObjectBase $object);
}

// display ObjectOne
class DisplayObjectOne extends DisplayBase {
    public function display(ObjectOne $object){
        print_r($object);
    }
}

// display ObjectTwo
class DisplayObjectTwo extends DisplayBase {
    public function display(ObjectTwo $object){
        var_export($object);
    }
}

// ObjectBase
class ObjectBase {
}

// ObjectOne
class ObjectOne extends ObjectBase {
    public $prop = 'I am one';
}

// ObjectTwo
class ObjectTwo extends ObjectBase {
    public $prop = 'I am two';
}
?>

According to the doc
<?php
function foo(ClassName $object) {
    // ...
}
?>
is equivalent to:
<?php
function foo($object) {
    if (!($object instanceof ClassName)) die("Argument 1 must be an instance 
of ClassName");
}
?>

but if you were to run this
<?php
// ObjectBase
class ObjectBase {
}

// ObjectTwo
class ObjectTwo extends ObjectBase {
    public $prop = 'I am two';
}

$ot = new ObjectTwo();
var_dump($ot instanceof ObjectBase);
var_dump($ot instanceof ObjectTwo);
?>
you would see : bool(true) bool(true)

so in theory, you should be able to rewrite the display classes as (I know 
abstract classes can't have a body, but image this made sense):
<?php
// base display class
abstract class DisplayBase {
    abstract public function display($object){
        if (!($object instanceof ObjectBase))  die("Argument 1 must be an 
instance of ObjectBase");
    }
}

// display ObjectOne
class DisplayObjectOne extends DisplayBase {
    public function display($object){
        if (!($object instanceof ObjectOne))  die("Argument 1 must be an 
instance of ObjectOne");
        print_r($object);
    }
}

// display ObjectTwo
class DisplayObjectTwo extends DisplayBase {
    public function display($object){
        if (!($object instanceof ObjectTwo))  die("Argument 1 must be an 
instance of ObjectTwo");
        var_export($object);
    }
}
?>

does anyone agree this is a bug, if not, can you explain why this voilates 
the OO-model of PHP5 

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