Reputation: 27899
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?
Upvotes: 4
Views: 311
Reputation: 1491
Using Demo::$name
and self::$name
from within the Demo
class 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 Person1
object, not a Person2
object (see: $p2=new Person1("xxx", "yyy");
)
If you were to create a Person2
object, 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 in name
and address
, but not static). If you declared them as static
, then you'd get the same output from the modified show
method 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 Person2
they'd all have access to the same name
variable, for example), whereas instance variables are unique to the individual object itself.
Upvotes: 2
Reputation: 5523
In your first example self::$name
and Demo::$name
are effectively the same if used inside the Demo
class.
Your second example should result in an error, since you haven't declared static $name
and $address
properties and are accessing them. It works in your case because you're creating an instance of Person1
(possibly unintentionally):
$p2=new Person1("xxx", "yyy");
Upvotes: 1