Bug #67544 [NEW]: Inheritance not working with typehints.
| From: | RQuadling at GMail dot com | Date: | Mon, 30 Jun 2014 11:16:54 +0000 |
| Subject: | Bug #67544 [NEW]: Inheritance not working with typehints. | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-186389@lists.php.net to get a copy of this message | ||
From: RQuadling at GMail dot com
Operating system: Win7
PHP version: 5.4.30
Package: *General Issues
Bug Type: Bug
Bug description:Inheritance not working with typehints.
Description:
------------
Type hinting doesn't support inheritance.
The error reported says "must be compatible".
Well, if I use the \Base\Item typehint for the
\Real\Collection\showItem() method, the code runs without error, so the
type being passed is fine. But I don't want any old implementation of
\Base\Item in the \Real\Collection. I want a \Real\Item which extends
from \Base\Item, so, to me, it IS compatible.
Using 'interface Collection' and 'implements \Base\Collection' rather
than abstracts makes no difference. Just in case there was anything
there.
Test script:
---------------
<?php
namespace Base;
abstract class Item {}
abstract class Collection {
abstract public function showItem(Item $Item);
}
namespace Real;
class Item extends \Base\Item {}
class Collection extends \Base\Collection {
public function showItem(Item $Item) {
echo get_class($Item);
}
}
namespace Usage;
$instance = (new \Real\Collection)->showItem(new \Real\Item);
Expected result:
----------------
Real\Item
Actual result:
--------------
Fatal error: Declaration of Real\Collection::showItem() must be
compatible with Base\Collection::showItem(Base\Item $Item) in C:\ns.php
on line 13
--
Edit bug report at https://bugs.php.net/bug.php?id=67544&edit=1
--