views:

34

answers:

2

Could anyone tell me what I can include in the 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.

+1  A: 

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

Ben James
+1  A: 

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';
}
cballou
Thanks. Can I call echo $variable_local in a view? How can I call $this->protected in a view?
shin