note 87731 added to language.oop5.typehinting

From: Date: Thu, 18 Dec 2008 15:41:54 +0000
Subject: note 87731 added to language.oop5.typehinting
Groups: php.notes 
Request: Send a blank email to php-notes+get-148318@lists.php.net to get a copy of this message
It has already been mentioned here that you can use 'self' as a typehint (in methods of classes and interfaces, not in functions of course), which is useful in interfaces and abstract method declarations to force the implementor to accept an instance of it's own type: <?php interface Foo { public function baz(self $object); } class Bar implements Foo { public function baz(self $object) { // } } ?> What has not been mentioned by now is that you can use 'parent' as a typehint too. Example with an interface: <?php interface Foo { public function baz(parent $object); } class Baz {} class Bar extends Baz implements Foo { public function baz(parent $object) { // } } ?> Bar::baz() will now accept any instance of Baz. If Bar is not a heir of any class (no 'extends') PHP will raise a fatal error: 'Cannot access parent:: when current class scope has no parent'. ---- Server IP: 69.147.83.197 Probable Submitter: 87.79.200.243 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php Edit -- https://master.php.net/note/edit/87731 Del: integrated -- https://master.php.net/note/delete/87731/integrated Del: useless -- https://master.php.net/note/delete/87731/useless Del: bad code -- https://master.php.net/note/delete/87731/bad+code Del: spam -- https://master.php.net/note/delete/87731/spam Del: non-english -- https://master.php.net/note/delete/87731/non-english Del: in docs -- https://master.php.net/note/delete/87731/in+docs Del: other reasons-- https://master.php.net/note/delete/87731 Reject -- https://master.php.net/note/reject/87731 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#148318) next »