Use $this to refer to 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();
This produces the following output.
Name : Tiny
Name : Tiny
Age : 13
What is the difference between self::$name and Demo::$name in the preceding 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();
Preceding two classes Person1 and Person2 produce the same output as follows.
Name : xxx
Address : yyy
What is the difference between (as in the Preson1 class, the show() method)
$this->name;
$this->address;
and (as in the Preson2 class, the show() method)
self::$name;
self::$address;
in this context?
Using
Demo::$nameandself::$namefrom within theDemoclass is, in effect, the same. You can access it by name due to the fact that the the variable is static and accessible, just like you’d be able to access a public static variable from a different class using the classes’ name, for example.So in that case, accessing by name is mostly just a by-product of being able to access any public static variable from an accessible class.
The second output produces the same as the first because you’re apparently creating a new
Person1object, not aPerson2object (see:$p2=new Person1("xxx", "yyy");)If you were to create a
Person2object, then you’d get an error, because you can’t assign values to a static variable that hasn’t been declared (you’ve got object-level variables innameandaddress, but not static). If you declared them asstatic, then you’d get the same output from the modifiedshowmethod due to the fact that it’s using a static call.If you’re not sure what the difference between object (read: instance) variables and static variables are, then i encourage you to partake in some Googling, but basically static variables exist at the class level, not the object level, and are accessible from any class method or object of the specified type (so if you had 100 instances of
Person2they’d all have access to the samenamevariable, for example), whereas instance variables are unique to the individual object itself.