#37854 [NEW]: Type Hinting with derived classes and interfaces not working

From: Date: Mon, 19 Jun 2006 22:11:36 +0000
Subject: #37854 [NEW]: Type Hinting with derived classes and interfaces not working
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-98528@lists.php.net to get a copy of this message
From:             spam at codeword dot net
Operating system: Win XP
PHP version:      5.1.4
PHP Bug Type:     Class/Object related
Bug description:  Type Hinting with derived classes and interfaces not working 

Description:
------------
When adding type hinting to an interface then implementing that interface
using a derived class, the script compiler does not seem to recognise that
the implemented type hint is derived from the interfaces type hint.  I hope
that makes sense.  The code below may be more clear.

Reproduce code:
---------------
<?php
interface MyInterface{public function test(ClassA $object);}
class MyClass implements myInterface{
  public function test(ClassB $object){// should work but does not
    echo $object->x;
  }
}
class ClassA {public$x = "Class is ClassA";  }
class ClassB extends ClassA{  public $x = "Class is ClassB";}
$myclass = new MyClass;
$a = new ClassA;
$b = new ClassB;
$myclass->test($b);
$myclass->test($a);
?>

Expected result:
----------------
Class is ClassB
Fatal error: Argument 1 passed to MyClass::test() must be an instance of
ClassB, called in typeHintTest.php on line 14 and defined in
typeHintTest.php on line 4

Actual result:
--------------
Fatal error: Declaration of MyClass::test() must be compatible with that
of MyInterface::test() in typeHintTest.php on line 3

-- 
Edit bug report at http://bugs.php.net/?id=37854&edit=1
-- 
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=37854&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=37854&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=37854&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=37854&r=fixedcvs
Fixed in release:             http://bugs.php.net/fix.php?id=37854&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=37854&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=37854&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=37854&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=37854&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=37854&r=notwrong
Not enough info:              http://bugs.php.net/fix.php?id=37854&r=notenoughinfo
Submitted twice:              http://bugs.php.net/fix.php?id=37854&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=37854&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=37854&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=37854&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=37854&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=37854&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=37854&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=37854&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=37854&r=mysqlcfg



Thread (5 messages)

« previous php.bugs (#98528) next »