Using "this" and "self" in PHP
Use $ this to indicate the current object. Use self to refer to the current class. In other words, use $ this-> member for non-static members, use self :: $ member for static members.
class Demo { private static $name; private $age; public function __construct($name, $age) { self::$name = $name; $this->age=$age; } public function show() { echo "Name : ", self::$name, "<br/>"; //Accessing by self echo "Name : ", Demo::$name, "<br/>"; //Accessing by class name echo "Age : ", $this->age, "<br/>"; } } $demo = new Demo("Tiny", 13); $demo->show(); The following conclusion is issued here.
Name : Tiny Name : Tiny Age : 13 What is the difference between self::$name and Demo::$name in the previous snippet?
class Person1 { private $name; private $address; public function __construct($name,$address) { $this->name = $name; $this->address = $address; } public function show() { echo "Name : ", $this->name, "<br/>"; echo "Address : ", $this->address, "<br/>"; //Accessing by this } } $p1=new Person1("xxx", "yyy"); $p1->show(); class Person2 { private $name; private $address; public function __construct($name,$address) { self::$name = $name; self::$address = $address; } public function show() { echo "Name : ", self::$name, "<br/>"; echo "Address : ", self::$address, "<br/>"; //Accessing by self } } $p2=new Person1("xxx", "yyy"); $p2->show(); The previous two classes Person1 and Person2 produce the same result as follows.
Name : xxx Address : yyy What is the difference between (as in Preson1 class, show() method)
$this->name; $this->address; and (as in the Preson2 class, show() method)
self::$name; self::$address; in this context ?
Using Demo::$name and self::$name from the Demo class is essentially the same. You can access it by name because the variable is static and accessible, just as you could access a public static variable from another class using the class name, for example.
So, in this case, access by name is basically just a by-product of accessing any public static variable from an accessible class.
The second output produces the same thing as the first, because you are apparently creating a new Person1 object, not a Person2 object (see $p2=new Person1("xxx", "yyy"); )
If you need to create a Person2 object, you will get an error because you cannot assign values ββto a static variable that has not been declared (you have object level variables in name and address , but not static). If you declared them as static , then you will get the same result from the modified show method due to the fact that it uses a static call.
If you do not know what the difference is between object variables (read: instance) and static variables, then I recommend you participate in some kind of Googling, but mostly static variables exist at the class level, not the object level, and are accessible from any class a method or object of the specified type (therefore, if you had 100 instances of Person2 , all of them would have access to the same variable name , for example), while instance variables are unique to a separate object itself.
In your first example, self::$name and Demo::$name actually the same if they are used inside the Demo class.
The second example should lead to an error, since you did not specify the static properties $name and $address and are accessing them. It works in your case, because you are creating an instance of Person1 (possibly unintentionally):
$p2=new Person1("xxx", "yyy");