note 64445 added to language.oop5.typehinting

From: Date: Fri, 14 Apr 2006 14:15:56 +0000
Subject: note 64445 added to language.oop5.typehinting
Groups: php.notes 
Request: Send a blank email to php-notes+get-108073@lists.php.net to get a copy of this message
Hi BKuker, it works in version 5.1.2-1ubuntu2: -------------------------------------------------------------- Code: <pre> <?PHP class cRight { } class cWrong { } class cTest { function bla(cRight $r = null) { if (!isset($r)) { echo "nothing\\n"; } else { var_dump($r); } } } $mysqli = new mysqli(); $right = new cRight(); $wrong = new cWrong(); $test = new cTest(); $test->bla($right); $test->bla(null); $test->bla(); $test->bla($wrong); ?> -------------------------------------------------------------- gives the result: object(cRight)#2 (0) { } nothing nothing Fatal error: Argument 1 passed to cTest::bla() must be an instance of cRight, ... -------------------------------------------------------------- You must add " = null " to the type hint, which is reasonable in my eyes. So as a coder of the method bla(), you can rely that you get a real object, unless you explicitely allow to get null in the method declaration, in which case you have to check, if you get a real object. ---- Server IP: 66.163.161.117 Probable Submitter: 88.72.234.4 ---- X-Spam-Status: No, hits=4.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_30_40,HTML_MESSAGE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php Edit -- http://master.php.net/note/edit/64445 Del: integrated -- http://master.php.net/note/delete/64445/integrated Del: useless -- http://master.php.net/note/delete/64445/useless Del: bad code -- http://master.php.net/note/delete/64445/bad+code Del: spam -- http://master.php.net/note/delete/64445/spam Del: non-english -- http://master.php.net/note/delete/64445/non-english Del: in docs -- http://master.php.net/note/delete/64445/in+docs Del: other reasons-- http://master.php.net/note/delete/64445 Reject -- http://master.php.net/note/reject/64445 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#108073) next »