Home > Backend Development > PHP Tutorial > Is there any difference between the init method in Yaf that instantiates multiple classes at the beginning and stores them in private properties and the need to create a new class again?

Is there any difference between the init method in Yaf that instantiates multiple classes at the beginning and stores them in private properties and the need to create a new class again?

WBOY
Release: 2016-10-22 00:14:14
Original
1638 people have browsed it

Note: init in Yaf is understood by default as __construct()

<code>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();
    }
}</code>
Copy after login
Copy after login
Is

any different from the following writing?

<code>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();
    }
}</code>
Copy after login
Copy after login

I am a newbie, I hope to have good programming habits, but some basics are not solid, so I hope you can give me some advice

Reply content:

Note: init in Yaf is understood by default as __construct()

<code>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();
    }
}</code>
Copy after login
Copy after login
Is

any different from the following writing?

<code>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();
    }
}</code>
Copy after login
Copy after login

I am a newbie, I hope to have good programming habits, but some basics are not solid, so I hope you can give me some advice

Writing like the above can ensure a singleton in the current class scope and reduce repeated writing of new. However, it is not suitable to write here for classes that need to be instantiated when used, and if your class is developed following the same interface, When replacing, you only need to modify new in init. Anyway, I still like to have Ioc containers

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template