Home > Backend Development > PHP Tutorial > 调用子类的test2方法时不去自动调用父类的构造方法 调用子类的test方法需要自动调用父类的构造方法

调用子类的test2方法时不去自动调用父类的构造方法 调用子类的test方法需要自动调用父类的构造方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:21:34
Original
1272 people have browsed it

namespace Home\Controller;
use Home\Common\BBaseController;
class IndexController extends BBaseController{

<code>public function __construct(){
    parent::__construct();
}

public function test(){
    echo "自动调用父类的构造方法";
}

public function test2(){

    echo "人为控制不去调用父类的构造方法";
}</code>
Copy after login
Copy after login

}

namespace Home\Common;
use Think\Controller;
class BBaseController extends Controller{

<code>public static $login = true;//对某些访问公开
public function __construct(){
    parent::__construct();
    $this->initialize();
}
</code>
Copy after login
Copy after login
<code>public function initialize(){
     if(!session("admin_home_user")){
        $this->error("您还没有登录,请登录","/Home/login",'3');
     }
}</code>
Copy after login
Copy after login

}

回复内容:

namespace Home\Controller;
use Home\Common\BBaseController;
class IndexController extends BBaseController{

<code>public function __construct(){
    parent::__construct();
}

public function test(){
    echo "自动调用父类的构造方法";
}

public function test2(){

    echo "人为控制不去调用父类的构造方法";
}</code>
Copy after login
Copy after login

}

namespace Home\Common;
use Think\Controller;
class BBaseController extends Controller{

<code>public static $login = true;//对某些访问公开
public function __construct(){
    parent::__construct();
    $this->initialize();
}
</code>
Copy after login
Copy after login
<code>public function initialize(){
     if(!session("admin_home_user")){
        $this->error("您还没有登录,请登录","/Home/login",'3');
     }
}</code>
Copy after login
Copy after login

}

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template