Re: Container or Calling Class

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

 



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
  }

}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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