Accessing inherited class variables in PHP
I have two classes. Looking to be able to grab a variable that is within a global object of a class.
Class Order {
public $number = "1234";
}
Class Business {
public $order;
function __construct() {
global $order;
$order = new Order();
}
}
$b = ne开发者_Go百科w Business();
echo $b->order->number;
In the case above nothing is displayed not even an error. Ive tried different ways of accessing the variable but have only been successful by making a helper function to make a call like the following:
echo $b->getOrder()->number;
or
$temp = $b->order;
echo $temp->number;
Both give the required result of "1234" however I am sure there is a way to do it in 1 line without having to make a getter function.
Any help would be greatly appreciated.
To access class variables you need to use $this->
Class Order {
public $number = "1234";
}
Class Business {
public $order;
function __construct() {
$this->order = new Order();
}
}
$b = new Business;
echo $b->order->number;
精彩评论