Could someone give me a brief explanation of the “this” command, as I am utterly confused
Thank you in advance for your response
Could someone give me a brief explanation of the “this” command, as I am utterly confused
Thank you in advance for your response
There is no ‘this’ command.
$this is a special variable inside of a class definition. $this is used to refer to the instance of the object implicitly, without needing to know the instantiation name.
For example.
class HelloWorld {
public $msg;
function __construct($in) {
$this->msg = $in;
}
}
So, i’ve got a class. Named HelloWorld. When I create an instance of my HelloWorld object, I have to specify a message. This message could be anything, and can be different for each instance of HelloWorld.
$bunny = new HelloWorld("Bunnies are cool.");
$lion = new HelloWorld("Bunnies are tasty.");
Note that the class definition has no idea that i’ve named my object variables $bunny and $lion… and it doesnt care, either.
echo $lion->msg; //Bunnies are tasty.