I am trying to reference a public variable from inside a
class.
class
Settings{
public $CompanyName = "MyWebsite"; // company name
public $PageTitle = "$this->CompanyName - big website"; // E.g. My Big
Website
}
But
this gives back a parse
error:
Parse error: parse
error
What
is the correct way of doing this?
class="normal">Answer
You can't
set variable with other variable when defining. Use href="http://php.net/manual/en/language.oop5.decon.php"
rel="nofollow">__construct for
it:
class Settings{
public $CompanyName = "MyWebsite"; // company name
public $PageTitle; // E.g.
My Big Website
public function
__construct(){
$this->PageTitle = $this->CompanyName." - big
website";
}
}
No comments:
Post a Comment