myd07
myd07

Reputation: 105

$this function in PHP with multiple arrow operator ->

I am fairly new in PHP and want to ask a question. I know if you want to access a properties or method within a class or object you can use $this->properties or $this->method.

But I encountered a code like this $this->context->cart

Can someone please explain the meaning behind this code ?

If there is another similar question already being asked in SO can you please provide me the link, I will check it out.

Big Thanks

Upvotes: 1

Views: 3035

Answers (2)

MahdiY
MahdiY

Reputation: 1306

See and try it:

class class1 {

    public $properties;
    public $context;

    function __construct(){

        $this->properties = '$properties in class1';
        $this->context = new class2(); // instance of class2

    }
}

class class2 {

    public $cart;

    function __construct(){

        $this->cart = '$cart in class2';

    }

}

$obj = new class1();

echo $obj->properties;
echo $obj->context->cart;

Upvotes: 4

Nishit Manjarawala
Nishit Manjarawala

Reputation: 461

context is instance of another class for retrive method or property of that class this is use

Upvotes: 0

Related Questions