I’m trying to figure out the difference between $_data vs $this->_data
class ProductModel
{
var $_data = null; <--- defined here
function test()
{
$this->_data = <--- but it's accessed using $this
}
}
I know in PHP var is used to define class properties but Why is it accessed using $this. Shouldn’t it be like $this->$_data ? What’s OOP concept is being used here ? Is it a PHP specific?
PHP along with several other popular programming languages such as Java (it’s important to note that PHP’s Object Oriented choices were at least partially inspired by Java) refer to the current object instance in context as
this. You can think ofthis, (or$this in PHP) as the “current object instance.”Inside of class methods,
$thisrefers to the current object instance.A very small example using what you have above:
The above will output
somethingsome other thing. Class members are stored along with the object instance, but local variables are only defined within the current scope.