note 110887 added to language.oop5.typehinting

From: Date: Wed, 19 Dec 2012 15:53:20 +0000
Subject: note 110887 added to language.oop5.typehinting
Groups: php.notes 
Request: Send a blank email to php-notes+get-192603@lists.php.net to get a copy of this message
I've spent some time reading all posts and I haven't find answer to my rare question. So here it is. In manual we read, that you could pass to method with type hint only objects of class that is the same as type hint or extend, or implement it (if type hint is Interface). But what happens if I want to override methods with type hint? I've made all possible tests with all combinations: - two classes for type hinting purposes - parentTH and childTH; - two base classes that use parentTH and childTH for type hinting in their methods - baseWithParentTH and baseWithChildTH; - exending classes, that extend baseWithParentTH and baseWithChildTH, override its method with all combinations of parentTH and childTH; - call overriden functions in all exending classes giving them parentTH and childTH. My code is realy long, so I will only post my results. I. COMPILER ERRORS When we try to override parent's method with own type hint that is different from parent's type hint PHP Strict warning occurs: <?php //PHP Strict Standards: Declaration of ExtendedOverridesWithParentTH::testTH() should be compatible with BaseWithChildTH::testTH(ChildTH $obj) ?> II. RUNTIME ERRORS Had 4 catchable fatal errors, that should be familiar to you: <?php //PHP Catchable fatal error: Argument 1 passed to BaseWithChildTH::testTH() must be an instance of ChildTH, instance of ParentTH given ?> Cases when errors occured: 1 - type hint in method required childTH and we tried to give it object of parentTH. That's wrong, we can do only in opposite direction - give a child if a parent is type hinted. 2 - in base class we have parentTH type hint and in extending class we have childTH type hint. We than launch object's method giving parentTH object as argument. Error occurs - we again try to give him parent while he only accepts child! 3 - in base class we have childTH type hint and in extending class we have parentTH type hint. We than launch object's method giving parentTH object as argument. Type hint check passes because our extending class expects parentTH or int's descendants. But in test method there is a call to base class's method with argument of parentTH class. That's where error uccurs - base class expects childTH to be given. Reason is the same as in cases 1 and 2. Let's summarize: 1. If you override with same type hint - everything works fine. 2. You CAN override base class's method with different type hint but be ready to get E_STRICT warning about those differencies. 3. If you realy NEED to override with different type hint - be careful! Overriden method will only accept object of class you mentioned when overrode or it's descendants. Forget about parent's type hint. 4. If you realy NEED to call parent's method you've overriden with different type hint, it could lead you to troubles. My advice not to use parent's call at all or you will have unstable code. ---- Server IP: 69.147.83.199 Probable Submitter: 79.165.147.233 ---- Manual Page -- http://php.net/manual/en/language.oop5.typehinting.php Edit -- https://master.php.net/note/edit/110887 Del: integrated -- https://master.php.net/note/delete/110887/integrated Del: useless -- https://master.php.net/note/delete/110887/useless Del: bad code -- https://master.php.net/note/delete/110887/bad+code Del: spam -- https://master.php.net/note/delete/110887/spam Del: non-english -- https://master.php.net/note/delete/110887/non-english Del: in docs -- https://master.php.net/note/delete/110887/in+docs Del: other reasons-- https://master.php.net/note/delete/110887 Reject -- https://master.php.net/note/reject/110887 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#192603) next »