开发者

What can I add in constructors in PHP?

开发者 https://www.devze.com 2022-12-16 14:51 出处:网络
Could anyone tell me what I can include in the开发者_C百科 constructor? I know that I can do the following.

Could anyone tell me what I can include in the开发者_C百科 constructor?

I know that I can do the following.

function  __construct(){
    parent::Controller();
    session_start();

  }

But I am wondering if I can add any variables, if statement etc.

Thanks in advance.


Knock yourself out. Add any PHP you want. You can use $this to refer to the object being created.


You can include variables, function calls, method calls, object declarations, etc, etc, etc inside your default constructor.

class Test {

    protected $protected;
    private static $static;

    function  __construct() {
        parent::__construct();
        $this->protected = 'test';
        $variable_local = 'hey';
        self::$static = 'im static';
        $obj = new OtherClass();
        $this->myMethod();
        externalFunction();
    }

    public function myMethod() {
        echo 'all mine';
    }

}

function externalFunction() {
    'hey, im external';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消