I need help in understanding the following php code.
$this->_PageHeader = new PAGE_HEADER($this);
I want to understand the functionality of $this argument in PAGE_HEADER($this).
I knew that $this is used inside a class to refer property and methods but in this case, what values will be passed through this argument.
$this
contains the current instance of the class you are in. So it means that you pass the current object of the class containing the code $this->_PageHeader = new PAGE_HEADER($this);
to the constructor of the class PAGE_HEADER
.
For instance:
class A
{
public $value = 1;
public function execute()
{
$b = new B($this);
}
}
class B
{
public $value = 2;
public function __construct(A $dep)
{
echo $dep->value; // 3
echo $this->value; // 2
}
}
$a = new A();
$a->value = 3;
$a->execute(); // In this execution, $this is the object $a.
This code will output 32.