note 95813 deleted from language.types.object by danbrown
| From: | danbrown@php.net | Date: | Sat, 23 Jan 2010 08:04:35 +0000 |
| Subject: | note 95813 deleted from language.types.object by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165226@lists.php.net to get a copy of this message | ||
Note Submitter: e dot sand at elisand dot com
----
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.