I’ve just started getting familiarized with OO features of PHP, and I would like to ask you something about the $this variable. First of all, if a class that I’m using the $this keyword in does not have a defined property variable foo, does that mean that using the following code:
$this->foo = 5;
echo $this->foo;
will create the foo property on the object on runtime, like in JavaScript? What is the visibility of this property?
Yes, this will create the
fooproperty, and its visibility will bepublic(which is the default).You could test this quite easily:
Will print 5 with no errors.