Additionally since $this:: has not been discussed yet.
For informational purposes only, as of PHP 5.3 when dealing with instantiated objects to get the current scope value, as opposed to using static::, one can alternatively use $this:: like so.
class Foo
{
const NAME = 'Foo';
//Always Foo::NAME (Foo) due to self
protected static $staticName = self::NAME;
public function __construct()
{
echo $this::NAME;
}
public function getStaticName()
{
echo $this::$staticName;
}
}
class Bar extends Foo
{
const NAME = 'FooBar';
/**
* override getStaticName to output Bar::NAME
*/
public function getStaticName()
{
$this::$staticName = $this::NAME;
parent::getStaticName();
}
}
$foo = new Foo; //outputs Foo
$bar = new Bar; //outputs FooBar
$foo->getStaticName(); //outputs Foo
$bar->getStaticName(); //outputs FooBar
$foo->getStaticName(); //outputs FooBar
Using the code above is not common or recommended practice, but is simply to illustrate its usage, and is to act as more of a "Did you know?" in reference to the original poster's question.
It also represents the usage of $object::CONSTANT for example echo $foo::NAME; as opposed to $this::NAME;
The object pointer $this to refers to the current object.
The class value "static" refers to the current object.
The class value "self" refers to the exact class it was defined in.
The class value "parent" refers to the parent of the exact class it was defined in.
See the following example which shows overloading.
<?php
class A {
public static function newStaticClass()
{
return new static;
}
public static function newSelfClass()
{
return new self;
}
public function newThisClass()
{
return new $this;
}
}
class B extends A
{
public function newParentClass()
{
return new parent;
}
}
$b = new B;
var_dump($b::newStaticClass()); // B
var_dump($b::newSelfClass()); // A because self belongs to "A"
var_dump($b->newThisClass()); // B
var_dump($b->newParentClass()); // A
class C extends B
{
public static function newSelfClass()
{
return new self;
}
}
$c = new C;
var_dump($c::newStaticClass()); // C
var_dump($c::newSelfClass()); // C because self now points to "C" class
var_dump($c->newThisClass()); // C
var_dump($b->newParentClass()); // A because parent was defined *way back* in class "B"
Most of the time you want to refer to the current class which is why you use static or $this. However, there are times when you needself because you want the original class regardless of what extends it. (Very, Very seldom)
According to http://www.php.net/manual/en/language.oop5.static.php there is no $self. There is only $this, for referring to the current instance of the class (the object), and self, which can be used to refer to static members of a class. The difference between an object instance and a class comes into play here.
Additionally since
$this::
has not been discussed yet.For informational purposes only, as of PHP 5.3 when dealing with instantiated objects to get the current scope value, as opposed to using
static::
, one can alternatively use$this::
like so.http://ideone.com/7etRHy
Using the code above is not common or recommended practice, but is simply to illustrate its usage, and is to act as more of a "Did you know?" in reference to the original poster's question.
It also represents the usage of
$object::CONSTANT
for exampleecho $foo::NAME;
as opposed to$this::NAME;
Inside a class definition, $this refers to the current object, while self refers to the current class.
It is necessary to refer to a class element using self, and refer to an object element using $this.
See the following example which shows overloading.
Most of the time you want to refer to the current class which is why you use
static
or$this
. However, there are times when you needself
because you want the original class regardless of what extends it. (Very, Very seldom)According to http://www.php.net/manual/en/language.oop5.static.php there is no
$self
. There is only$this
, for referring to the current instance of the class (the object), and self, which can be used to refer to static members of a class. The difference between an object instance and a class comes into play here.