Home >Backend Development >PHP Tutorial >Explanation of the difference between _initialize() function and __construct() function in PHP

Explanation of the difference between _initialize() function and __construct() function in PHP

不言
不言Original
2018-08-18 11:35:1815773browse

This article brings you an explanation of the difference between the _initialize() function and the __construct() function in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

_initialize() method must be executed before any method is executed, including of course __construct constructor. That is to say, if there is a _initialize() function, calling any method of the object will cause the _initialize() function to be automatically called, and the __construct() constructor is only called once when the object is created, and has nothing to do with other method calls.

__construct here is a double dash, and the _initialize() function is a single dash

If both the parent and child classes have the _initialize() function, the child class overrides the parent class. If If the subclass does not have it but the parent class does, the subclass inherits it from the parent class.

By default, the constructor of the subclass will not automatically call the constructor of the parent class. When calling _initialize() of a subclass object, it will not automatically call _initialize() of the parent class.

When actually writing the constructor of the subclass, you generally need to add the constructor of the parent class. Actively call parent::__construct(), otherwise it will cause a null pointer exception of the subclass object, such as Call to a member function assign() on a non-object.

Therefore, in some system background management or comment functions, the relevant controller can be extended to the base controller:

Base.php:

<?php
namespace app\admin\controller;
use think\Controller;
class Base extends Controller
{
    public function _initialize(){
        if(!session(&#39;username&#39;)){
            $this->error('请先登录系统!','Login/index');
        }
    }
}

Article.php

<?php
namespace app\Admin\controller;
use app\Admin\model\Article as ArticleModel;
use app\admin\controller\Base;
class Article extends Base
{
   ……… ………
}

In this way, when any method of the Article controller is executed, the _initialize() method in Base.php will be executed first to detect whether you have logged in, without creating an object.

Related recommendations:

The difference between the explode() function and strtok() function in php

##method_exists( in php ) and is_callable() function analysis

The above is the detailed content of Explanation of the difference between _initialize() function and __construct() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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

Related articles

See more