note 64445 deleted from language.oop5.typehinting by bjori

From: Date: Fri, 14 Apr 2006 14:58:55 +0000
Subject: note 64445 deleted from language.oop5.typehinting by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-108084@lists.php.net to get a copy of this message
Note Submitter: phponlinedoc-a-t-krasse-d-o-t-info Reason: useless ---- 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.

« previous php.notes (#108084) next »