2014-07-17 114 views
0

什么是最好的方法调用方法heDidIt()从孩子类Make? 我在想事件,但无法找到一个好的非全局解决方案。报告创建者类php

$control = new Control(); 
$maker = $control->createMaker(); 
$maker->doIt(); 

class Control 
{ 
    private $_make; 
    public function createMaker() 
    { 
     $this->_make = new Make(); 
     return $this->_make; 
    } 

    private function heDidIt() 
    { 
     //Call me if the Maker did something. 
    } 

} 

class Make 
{ 
    public function doIt() 
    { 
     //hey im doing something, better tell my Controller 
    } 
} 

回答

2

只要告诉Make谁是它的老板,因此它可以通知他:

$control = new Control(); 
$maker = $control->createMaker(); 
$maker->doIt(); 

class Control 
{ 
    private $_make; 
    public function createMaker() 
    { 
     $this->_make = new Make($this); 
     return $this->_make; 
    } 

    private function heDidIt() 
    { 
     //Call me if the Maker did something. 
    } 

    public function inform($sampleParam) { 
     var_dump($sampleParam); 
     $this->heDidIt(); 
    } 
} 

class Make 
{ 
    protected $control; 

    public function __construct(Control $control) { 
     $this->control = $control; 
    } 

    public function doIt() 
    { 
     //hey im doing something, better tell my Controller 
     $control->inform('called in Make::doIt()'); 
    } 
} 
+0

是的,我会告诉他谁是老板! – bergman

0
$control = new Control(); 
$maker = $control->createMaker(); 
$maker->doIt(); 

class Control 
{ 
    private $_make; 
    public function createMaker() 
    { 
     $this->_make = new Make(); 
     return $this->_make; 
    } 

    **protected** function heDidIt() 
    { 
     //Call me if the Maker did something. 
    } 

} 

class Make **extends Control** 
{ 
    public function doIt() 
    { 
     **$this -> heDidIt();** 
     //hey im doing something, better tell my Controller 
    } 
} 

虽然这看起来非常没有意义的,所以也许提供您的实际代码,并要求将让我们帮你更好。

+1

heDidIt()是私有 – Phantom

+0

好对象,感谢 – t3chguy

+0

如果什么控制需要知道每'Make'did? – DarkBee