php - How do I make a class variable automatically available to its children classes? -
i have application
class , have number of separate classes extend it. if set $variable
in parent application
class, how make automatically available in children?
class application { public $variable; public function __construct() { $this->variable = "something"; } } class child extends application { public function dosomthing() { $mything = $variable." cool"; return $mything; } }
i know can put global $variable;
in dosomthing()
method, super tedious on , on in every method write. there way available child class methods? thanks.
you set property named variable
in application
class in __construct
method.
if property visibility permits (e.g. public or protected), can access property potato
in children classes method $this->potato
:
class application { public $variable; public function __construct() { $this->variable = "something"; } } class child extends application { public function dosomthing() { $mything = $this->variable." cool"; return $mything; } }
Comments
Post a Comment