views:

47

answers:

2
<?php
$sth = Framework::blah()->any_key['any_key_2'];
?>

Hello, I want to get 'any_key' and 'any_key_2' in blah(), how I do that?

A: 

That is not possible or you would need to pass these as a parameter to blah in some way.

The key concept that is used in method chaining or when implementing a fluent interface is to return the object itself in every method.

Gumbo
+4  A: 

What you are doing with Framework::blah()->any_key['any_key_2']; is this:

Statically call the method blah() in the Framework class. The method call has to return an object from which you can get the property any_key somehow. The value of any_key has to be an array or something that implements ArrayAccess.

class Framework
{
    public static function blah()
    {
        return new ArrayObject(
            array('any_key' => array(
                'any_key_2' => 'blablablah')
            ), ArrayObject::ARRAY_AS_PROPS);
    }
}

or

class Framework {

    public $any_key = array(
        'any_key_2' => 'blahblahblah'
    );

    public static function blah()
    {
        return new self;
    }
}

or

class Framework
{
    public static function blah()
    {
        $class = new StdClass;
        $class->any_key = new Foo;
        return $class;
    }
}

class Foo implements ArrayAccess
{
    protected $any_key_2 = 'blahblahblah';
    public function offsetGet ($offset){
        return $this->$offset;
    }
    public function offsetSet ($offset, $value){}
    public function offsetUnset ($offset){}
    public function offsetExists ($offset){}
}
Gordon