What can I add in constructors in PHP?
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';
}
精彩评论