Home  >  Article  >  php教程  >  Example analysis of _initialize method in thinkPHP

Example analysis of _initialize method in thinkPHP

高洛峰
高洛峰Original
2016-12-30 09:43:111220browse

The example in this article describes the _initialize method in thinkPHP. Share it with everyone for your reference, the details are as follows:

The _initialize method of the subclass automatically calls the _initialize method of the parent class. As for PHP's constructor construct, if you want to call a method of the parent class, you must explicitly call parent::__construct();

<?php
class BaseAction extends Action { // 继承Thinkphp
  protected function _initialize() { // thinkphp中的函数
    echo "Base class";
  }
}

If the subclass does not have an _initialize method in the subclass constructor , the _initialize method of the parent class is called by default.

Output: Base class

If there is, execute your own _initialize method.

Output: hello I am child

If executed simultaneously, write like this

<?php
class IndexAction extends BaseAction {
  public function _initialize(){ // 如果子类存在initialize,则不执行父类的内容
    parent::_initialize(); // 加上这一句,才执行父类的初始化函数
    echo "hello I am child";
  }
}


Output: Base class hello I am child

I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.

For more articles related to example analysis of the _initialize method in thinkPHP, please pay attention to 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