Re: echo returnArray()['a']; // workaround
| From: | Nathan Nobbe | Date: | Fri, 23 May 2008 04:54:46 +0000 |
| Subject: | Re: echo returnArray()['a']; // workaround | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-274645@lists.php.net to get a copy of this message | ||
On Fri, Feb 29, 2008 at 12:16 AM, Nathan Nobbe <quickshiftin@gmail.com>
wrote:
> wow, im going to have to stare at some of those and play around with them
> as soon as im half awake :)
>
> of course i still like my solution ;) but im excited about the
> experimentation and ideas that have been shared on this topic, very
> interesting really!
i added __set() to my original class, now i can do cool stuff, like this:
$a = ArrayClass::simpleFactory(getArray())->{'a'} = 5;
which allows retrieval of the array, and modification (or access) to a given
member, in a single statement.
<?php
class ArrayClass {
private $theArray = array();
private function __construct($theArray) {
$this->theArray = $theArray;
}
public static function simpleFactory($theArray) {
return new self($theArray);
}
public function __get($member) {
if(array_key_exists($this->theArray, $member)) {
return $this->theArray[$member];
}
return null;
}
public function __set($member, $value) {
$this->theArray[$member] = $value;
}
}
function getArray() {
return array(
'a' => 1,
'b' => 2
);
}
$a = ArrayClass::simpleFactory(getArray())->{'a'} = 5;
var_dump($a);
?>
maybe boring to some (or many :D) but as the first time around, i just
thought it was cool and id share.
-nathan