开发者

Sharing Methods Between Child Objects

I'm trying to share methods between objects that belong to a parent object.

I have a main object, which has child objects that handle different tasks. These are created using the main objects constructor:

class engine {

  public f开发者_如何学JAVAunction __construct() {

    $this->db = new db();
    $this->url = new url();
    $this->template = new template();

  }

}

Here's an example of how I would use my main object:

$engine = new engine();

$engine->db->connect();
$engine->url->parse();
$engine->template->render();

How could the child objects access the methods of the other children (e.g. how could template->render() call url->parse()) ?


You can set static property of a class and call it staticaly:

static public function parse() {
    <some code>
}

and call it like Url::parse(); from $template->render();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜