php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36601 Inherited methods cannot specialize/change type hints
Submitted: 2006-03-03 09:57 UTC Modified: 2006-03-03 18:16 UTC
Votes:28
Avg. Score:4.4 ± 0.9
Reproduced:25 of 27 (92.6%)
Same Version:20 (80.0%)
Same OS:15 (60.0%)
From: webmaster at domiwitt dot de Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: webmaster at domiwitt dot de
New email:
PHP Version: OS:

 

 [2006-03-03 09:57 UTC] webmaster at domiwitt dot de
Description:
------------
On an inherited methods that uses parameter type hints it is not possible to specialize a type hint. By "specialize" I mean that the type hint on the inheriting class's method is able to type check an inherited class of the previous type hint.

I have found several cases, where this behaviour would be extemely useful. The Specification Pattern poses a good example.

Certainly you can work around it by not using type hints and do some "instanceof" or "get_class()" checking inside, throwing an exception in unwanted cases. It would however be nice if the language itself could support such a behaviour.

Reproduce code:
---------------
<?php

interface ISpecification {
    
    public static function isSpecifiedBy($object);
    
}

class FooSpecification implements ISpecification {

    public  static function isSpecifiedBy($object) {
        echo get_class($object);
    }

}

class BarSpecification implements ISpecification {

    public static function isSpecifiedBy($object) {
        echo get_class($object);
    }

}

class Foo { }

class Bar extends Foo { }

FooSpecification::isSpecifiedBy(new Foo());
BarSpecification::isSpecifiedBy(new Bar());

?>

Expected result:
----------------
For this not to throw a compile time error

Actual result:
--------------
Fatal error: Access level to FooSpecification::isSpecifiedBy() must be public (as in class ISpecification) in [...] on line 9

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-03 10:16 UTC] webmaster at domiwitt dot de
Now I know that this will not get fixed but I made a mistake in the coding example above. So this is just to state my original intention:

<?php

interface ISpecification {
    
    public static function isSpecifiedBy($object);
    
}

class FooSpecification implements ISpecification {

    public  static function isSpecifiedBy(Foo $object) {
        echo get_class($object);
    }

}

class BarSpecification implements ISpecification {

    public static function isSpecifiedBy(Bar $object) {
        echo get_class($object);
    }

}

class Foo { }

class Bar extends Foo { }


FooSpecification::isSpecifiedBy(new Foo());
BarSpecification::isSpecifiedBy(new Bar());

?>
 [2006-03-03 18:16 UTC] helly@php.net
Even if we were allowing to change the typehint in the derived class' method we would allow to specify a specialized type but a generalized type because we still would need to take inheritance rules into account.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jan 05 05:01:28 2025 UTC