In plain English, how is $this
used in PHP?
It's such a simple concept to me in JavaScript, but for some reason in PHP I can't get my head around this variable and its function. What, at any given point, is it referring to exactly? I have only tertiary experience with OOP, and I suspect that's why it's hard for me to understand its usage, but I'm trying to get better, and a lot of the code I examine uses this variable.
$this is a reserved keyword in PHP that refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object.
Whenever you want to use a variable that is outside of the function but inside the same class, you use $this. $this refers to the current php class that the property or function you are going to access resides in.
Definition and UsageThe extends keyword is used to derive a class from another class. This is called inheritance. A derived class has all of the public and protected properties of the class that it is derived from.
The use is necessary for your controller to be able to use ImageRepository . Once it is loaded into your object in the constructor with $this->image = $imageRepository; , your controller methods have access to its methods, (such as upload ) via $this->image .
Once inside an object's function, you have complete access to its variables, but to set them you need to be more specific than just using the variable name you want to work with. To properly specify you want to work with a local variable, you need to use the special $this
variable, which PHP always sets to point to the object you are currently working with.
For example:
function bark()
{
print "{$this->Name} says Woof!\n";
}
Whenever you are inside a function of an object, PHP automatically sets the $this
variable contains that object. You do not need to do anything to have access to it.
$this
is a pseudo-variable which is available when a method is called from within an object context. It is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object)
An example:
<?php
class A
{
function foo()
{
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ")\n";
} else {
echo "\$this is not defined.\n";
}
}
}
class B
{
function bar()
{
// Note: the next line will issue a warning if E_STRICT is enabled.
A::foo();
}
}
$a = new A();
$a->foo();
// Note: the next line will issue a warning if E_STRICT is enabled.
A::foo();
$b = new B();
$b->bar();
// Note: the next line will issue a warning if E_STRICT is enabled.
B::bar();
?>
Output:
$this is defined (A)
$this is not defined.
$this is defined (B)
$this is not defined.
To elaborate shamittomar:
$this is a handle to be able to refer to the current object where the call is done in. (so it basically points to itself. Say we have multiple objects of the same class, and we wanted to set it up with (different) data before we do the ultimate move: echo it. It would be hard to point to itself when you don't know the object name.
class SaySomething{
private $the_line;// the variable exists only in this class!
public function __construct($myline){
$this->the_line = $myline;
// see how it points to itself?
// would there be a variable in the global scope then it would be not possible to "setup"
// the object without it getting overwritten in the next setup.
}
//The function to echo the stuf. Can be callid by the "outside world"
public function say_it(){
echo $this->the_line;
$this->add_exclamation();//call the function add_exclamation in this class/object.
}
//This function can not be called by the outside world because it's private.
// The only way to call it is from inside this class. To point to this class and call the function:
// $this->add_exclamation();
private function add_exclamation(){
echo "!";
}
}
$obja = new SaySomething('my');
$objb = new SaySomething('sample');
$objc = new SaySomething('super');
$objd = new SaySomething('text');
//Mind: uptill nothing has been said, only the private variable $the_line has been set in the constructor.
$obja->say_it();
$objc->say_it();
$objb->say_it();
$objd->say_it();
To understand what a class and what an object is (they tend to be mixed up a lot...) watch this slideshow: http://www.slideshare.net/sebastian_bergmann/understanding-the-php-object-model
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With