views:

19

answers:

1

Hello, 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 = new 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.

+2  A: 

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;
Chacha102
Thanks Chacha102! This solves the problem.
aaronfarr