Re: Container or Calling Class

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

 



That's containment, not inheritence, must have misread the email.  Oops :)

The "easiest" way to do this would be something like:

class contrived {
private $parent;
private $otherparent;

public function __call($func, $params) {
if(is_callable(array($this->parent, $func))
call_user_func_array(array($this->parent, $func), $params)
elseif(is_callable(array($this->otherParent, $func))
call_user_func_array(array($this->otherparent, $func), $params)
}

}

This is the most "elegant" way to accomplish the same thing, but it makes
some assumptions, like you're willing to chain if-elseif's together (or
store all contained instances in an array) and that you don't ever have
overlapping method names (ie contrived.foo and parent.foo both exist).
Also, it's really, really obnoxiously slow.  __call() is hardly fast and
call_user_func_array is no different.

On Sun, May 24, 2009 at 7:52 PM, Nathan Rixham <nrixham@xxxxxxxxx> wrote:

> Eddie Drapkin wrote:
>
>> You can call methods from a classes's parents like so
>> class foo {
>>
>> protected method bar() {
>> echo "in foo!";
>> }
>>
>> }
>>
>> class foobar extends foo {
>>
>> public function bar() {
>> parent::bar();
>> }
>> }
>>
>> $fb  = new foobar();
>> $fb->bar(); will output "in foo!";
>>
>>
> wrong way round.. he's asking for:
>
> // note no extends
> class foobar {
>
>  public function bar() {
>    $this->foo = new Foo();
>    $this->foo->bar();
>  }
>
>  public function poo() {
>     echo "call me if you can";
>  }
>
> }
>
> // and the impossible
> class foo {
>
>  public method bar() {
>    foobar->poo(); // call the containing class
>  }
>
> }
>
> in a dom or as3 or suchlike this would be
>
> this.parent.poo();
>
> but we have not this->parent
>
> note: note parent::poo() which is effectively super.poo() <lol>
>
> so he can only inject the container/parent
>
> <?php
>
> class foobar {
>
>  private $foo;
>
>  public function bar() {
>    $this->foo = new Foo( $this );
>    $this->foo->bar();
>  }
>
>  public function poo() {
>     echo "call me if you can";
>  }
>
> }
>
> // and the Possible
> class foo {
>
>  private $parent;
>
>  public function __construct( $parent ) {
>    $this->parent = $parent;
>  }
>
>  public method bar() {
>    $this->parent->poo(); // call the containing class method poo
>  }
>
> }
>

[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