note 107246 added to language.oop5.magic

From: Date: Tue, 24 Jan 2012 14:58:32 +0000
Subject: note 107246 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-185727@lists.php.net to get a copy of this message
osbertv at yahoo dot com: the problem is not that B contains an instance of A, the problem is that "$b->a()" looks for a method named "a" on $b, and when one is not found, it calls B's __invoke(). This is an order of operations thing, the compiler couldn't hope to know whether you meant to call B::a() or B::$a(). The previous one can be compile-time optimized while the latter one cannot (since there is no guarantee that B::$a is always callable). When there is a collision of intent like this, the compiler tends to give priority to the optimizable use case over the unoptimizable use case. You can either choose to proxy a call to $this->a; inside of B::__invoke() (probably with something like: return call_user_func_array($this->a, func_get_args()); ), or you can make an actual method B::a() for the same purpose (which would be faster). However, either way, it's probably not a great idea to be trying to use properties as though they were methods. The code this would produce can be very confusing to read. Creating B::a() solves this readability problem. ---- Server IP: 69.147.83.197 Probable Submitter: 69.253.249.28 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- https://master.php.net/note/edit/107246 Del: integrated -- https://master.php.net/note/delete/107246/integrated Del: useless -- https://master.php.net/note/delete/107246/useless Del: bad code -- https://master.php.net/note/delete/107246/bad+code Del: spam -- https://master.php.net/note/delete/107246/spam Del: non-english -- https://master.php.net/note/delete/107246/non-english Del: in docs -- https://master.php.net/note/delete/107246/in+docs Del: other reasons-- https://master.php.net/note/delete/107246 Reject -- https://master.php.net/note/reject/107246 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185727) next »