note 56128 added to language.oop5.typehinting

From: Date: Wed, 24 Aug 2005 13:08:46 +0000
Subject: note 56128 added to language.oop5.typehinting
Groups: php.notes 
Request: Send a blank email to php-notes+get-94126@lists.php.net to get a copy of this message
Please note, that type-hinting does not mean type-safety! Although the parameter is forced to be of the hinted type, the function itself could do a call to any subtype's method without a parser error or warning. This might be unexpected for Java-Programmers. An example: I've build an interface, an implementing class and a little bit operating code. The class implements one additional method, this would be found normally in deriving classes (But I wanted to hold the example simple). The implementing class: <?php require_once("Testinterface.php"); class Test implements TestInterface { public function testingA() { echo "A"; } public function testingB() { echo "B"; } } ?> It's interface: <?php interface Testinterface { public function testingA(); // Note that testingB() is NOT defined here! } ?> And the testing-code: <?php require_once("Test.php"); require_once("Testinterface.php"); function foo(Testinterface $test) { $test->testingA(); $test->testingB(); } foo(new Test()); ?> Running the testing-code, you would get the following output: AB This was quit unexpected to me, I hoped for a compiler or parser-error, because my intention was it to hide testingB() from foo() - a common way to do in Java. So, Java-Programers, be prepared. ---- Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56128 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56128&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56128&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56128&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56128&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56128&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56128&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56128&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94126) next »