Re: echo returnArray()['a']; // workaround

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Feb 29, 2008 at 12:16 AM, Nathan Nobbe <quickshiftin@xxxxxxxxx>
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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux