In Yaf, the init method instantiates multiple classes at the beginning and stores them in private attributes. what is the difference between the init method and the new class?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.