Use self if you want to call a method of a class without creating an object/instance of that class, thus saving RAM (sometimes use self for that purpose). In other words, it is actually calling a method statically. Use this for object perspective.
I believe question was not whether you can call the static member of the class by calling ClassName::staticMember. Question was what's the difference between using self::classmember and $this->classmember.
For e.g., both of the following examples work without any errors, whether you use self:: or $this->
class Person{
private $name;
private $address;
public function __construct($new_name,$new_address){
$this->name = $new_name;
$this->address = $new_address;
}
}
class Person{
private $name;
private $address;
public function __construct($new_name,$new_address){
self::$name = $new_name;
self::$address = $new_address;
}
}
According to php.net there are three special keywords in this context: self, parent and static. They are used to access properties or methods from inside the class definition.
$this, on the other hand, is used to call an instance and methods of any class as long as that class is accessible.
Those are the results for 2 000 000 runs, and here is the code I used:
<?php
require '../vendor/autoload.php';
// My small class to do benchmarks
// All it does is looping over every test x times and record the
// time it takes using `microtime(true)`
// Then, the percentage is calculated, with 100% being the quickest
// Times are being rouned for outputting only, not to calculate the percentages
$b = new Tleb\Benchmark\Benchmark(2000000);
class Foo
{
public function calling_this()
{
$this->called();
}
public function calling_self()
{
self::called();
}
public function calling_static()
{
static::called();
}
public static function called()
{
}
}
$b->add('$this->', function () { $foo = new Foo; $foo->calling_this(); });
$b->add('self::', function () { $foo = new Foo; $foo->calling_self(); });
$b->add('static::', function () { $foo = new Foo; $foo->calling_static(); });
$b->run();
Use
self
if you want to call a method of a class without creating an object/instance of that class, thus saving RAM (sometimes use self for that purpose). In other words, it is actually calling a method statically. Usethis
for object perspective.I believe question was not whether you can call the static member of the class by calling
ClassName::staticMember
. Question was what's the difference between usingself::classmember
and$this->classmember
.For e.g., both of the following examples work without any errors, whether you use
self::
or$this->
According to php.net there are three special keywords in this context:
self
,parent
andstatic
. They are used to access properties or methods from inside the class definition.$this
, on the other hand, is used to call an instance and methods of any class as long as that class is accessible.From this blog post:
As no one here talked about performances, here is a small benchmark I did (5.6):
Those are the results for 2 000 000 runs, and here is the code I used:
self
refers current class(in which it is called),$this
refers current object. You can use static instead of self. See the example:Output: parent child