Friday, 17 November 2017

PHP Reference variable in a class





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="post-text" itemprop="text">
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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...