Note: in Yaf, init is understood as _ construct () {code...} by default. Is there any difference between init and the following statements? {Code...} is one of the following Cainiao, hoping to have a good programming habit. I still hope to give some guidance on the basics. note: The init in Yaf is understood
__construct()
class Order{ private $extend = ''; private $order = ''; public function init(){ parent::init(); $this->order = new Order(); $this->extend = new OrderEx(); } public function listAction(){ $this->order->public(); } public function makeAction(){ $this->extend->public(); }}
Is it different from the following?
class Order{ private $extend = ''; private $order = ''; public function init(){ parent::init(); } public function listAction(){ $order = new Order(); $order->public(); } public function makeAction(){ $extend= new extend(); $extend->public(); }}
I hope to have a good programming habit when I'm a newbie. I hope I can give some advice if I have a poor foundation.
Reply content:
Note: The init in Yaf is understood__construct()
class Order{ private $extend = ''; private $order = ''; public function init(){ parent::init(); $this->order = new Order(); $this->extend = new OrderEx(); } public function listAction(){ $this->order->public(); } public function makeAction(){ $this->extend->public(); }}
Is it different from the following?
class Order{ private $extend = ''; private $order = ''; public function init(){ parent::init(); } public function listAction(){ $order = new Order(); $order->public(); } public function makeAction(){ $extend= new extend(); $extend->public(); }}
I hope to have a good programming habit when I'm a newbie. I hope I can give some advice if I have a poor foundation.
Writing in the above way can ensure the Singleton in the current class scope and reduce repeated writing of new. However, it is not suitable for writing classes that need to be instantiated during use, if your class is developed based on the same interface, you only need to modify the new in init when changing the class. Anyway, I still like Ioc containers.