note 102790 deleted from function.compact by danbrown
| From: | danbrown@php.net | Date: | Thu, 14 Apr 2011 14:59:20 +0000 |
| Subject: | note 102790 deleted from function.compact by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178839@lists.php.net to get a copy of this message | ||
Note Submitter: ryan at punchkickinteractive dot com
----
There certainly is a time and place for compact(), but my official stance on it is that it should
never be nested within another function or class method.
I've written a simple example below to show how it does not evaluate as an array until AFTER
the outer method has been started.
<?php
/*
* RUN THIS SCRIPT...
* Outputs "Thomas the Cat"
*
* THEN UNCOMMENT THE LAST LINE...
* PHP Parse error: syntax error, unexpected '['
*/
$tom = "Thomas the Cat";
$jerry = "Jerry the Mouse";
$cat_and_mouse = compact('tom', 'jerry');
print_r($cat_and_mouse['tom']);
// UNCOMMENT THE NEXT LINE TO SEE THE 'compact' TEST
// print_r(compact('tom', 'jerry')['tom']);
?>