note 95813 added to language.types.object
| From: | e dot sand at elisand dot com | Date: | Sat, 23 Jan 2010 03:48:36 +0000 |
| Subject: | note 95813 added to language.types.object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165224@lists.php.net to get a copy of this message | ||
In response to an issue that Isaac Z. Schlueter i at foohack dot com brought up in an earlier
post...
If you have a situation where a function returns an array and you want to access a specific key
within that returned function all in one shot (to save assigning the result to a variable, and then
using the variable to access the array key), you may have thought of trying to cast it to an object.
However, you'll also notice that if you try to cast the result to an object and then access the
property all in one shot, that casting has lower precedence than the -> operator, so it
doesn't work.
The trick is quite simple - use settype() to cast your result:
$result = settype(some_function($input), 'object')->my_key;
As to whether this is "better" than just biting the bullet and saving the array to a
temporary variable is up for debate, but if you're set to avoid cluttering up your variable
space, this may be of use.
----
Server IP: 69.147.83.197
Probable Submitter: 174.115.178.57
----
Manual Page -- http://www.php.net/manual/en/language.types.object.php
Edit -- https://master.php.net/note/edit/95813
Del: integrated -- https://master.php.net/note/delete/95813/integrated
Del: useless -- https://master.php.net/note/delete/95813/useless
Del: bad code -- https://master.php.net/note/delete/95813/bad+code
Del: spam -- https://master.php.net/note/delete/95813/spam
Del: non-english -- https://master.php.net/note/delete/95813/non-english
Del: in docs -- https://master.php.net/note/delete/95813/in+docs
Del: other reasons-- https://master.php.net/note/delete/95813
Reject -- https://master.php.net/note/reject/95813
Search -- https://master.php.net/manage/user-notes.php