What is the difference between static
and self
? Exemplify with situations that justify their different uses. What does this have to do with late static binding ?
What is the difference between static
and self
? Exemplify with situations that justify their different uses. What does this have to do with late static binding ?
static
is used to define that a method or attribute in a class is static. This means (as someone who knows about OO must know) that that method / attribute belongs to the class and not to an instance of it and therefore can be accessed without instantiating a new object.
Example:
<?php
class Foo
{
public static $meu_estatico = 'foo';
public function valorEstatico() {
return self::$meu_estatico;
}
}
You can use something like:
print Foo::$meu_estatico . "\n";
Pure like this, without having to make a $a = new Foo()
before.
Notice in the example that self
was used, which was also questioned. It is only used to use a static variable within the class that contains it.
$a = new Foo();
echo $a->valorEstatico() // tem como saída "foo"
It is important to make it clear that a static value belongs to the class and not to the instances, but can be used within the class via self
.
This is very interesting for values that you want available for the entire application, for example. If they change in the class only once the whole application has access to the same values.
These examples have been taken from link
AFTER QUESTION UPDATE. . .
When a class uses a static method that was inherited from another, static values within this inherited method will reference the parent class if self
is used. To use the specialized class (daughter) as a reference for these static values, we use self static binding. Understanding binding as a "connection" (I do not know if it helps a lot). Citing the doc and an example:
This feature was named "late static bindings" with a lookup internal in mind. "Late binding" comes from the fact that static :: no longer be solved using the class where it is defined but it will be evaluated using runtime information. He was also called "static binding" as it can be used for (but not limited to) to) call static methods. ( link )
Examples (also from docs, but merge the two)
<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who(); // Isso vai sair como "A", o nome da classe mãe
}
public static function test2() {
static::who(); // Já esse aqui vai sair "B", o nome da classe filha
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test(); // Isso vai sair como "A", o nome da classe mãe
B::test2(); // Já esse aqui vai sair "B", o nome da classe filha
?>
self
is used to access properties of the class within itself, ie for all instances there will only be a single value since the property is class.
static
is a qualifier that generates a property of the class rather than a property of the object or instance of the class, the code below exemplifies the use of the two and also differentiates a property of a class object from a class property: p>
<?php
class X {
private $non_static_member = 1; //propriedade do objeto da classe
private static $static_member = 2; // propriedade da classe
function __construct() {
// Acessando propriedade do objeto da classe
echo $this->non_static_member . ' '
// Acessando propriedade da classe
. self::$static_member;
}
}
// Precisamos instanciar a classe para acessar as propriedades do objeto criado.
(new X())->$non_static_member;
// Acessamos a partir da classe.
X::static_member
?>
Basically late static binding is used to reference a specialization (daughter class) from an implementation performed in the generalized class (parent class), allowing polymorphism between the child classes.
<?php
class DartVader {
public static function say() {
echo "I'm your father";
}
public static function sayToLuky() {
self::say(); // "I'm your father"
}
public static function sayToDartVader() {
static::say(); // Can be "Han Solo my love!" or "Noooooo!"
}
}
class Luke extends DartVader {
public static function say() {
echo "Noooooo!";
}
}
class Leia extends DartVader {
public static function say() {
echo "Han Solo my love!";
}
}
Leia::sayToDartVader() // "Han Solo my love!"
Leia::sayToLuky() // "I'm your father"
Luke::sayToDartVader() // "Noooooo!"
Luke::sayToLuky() // "I'm your father"
?>
The keyword self
Accesses a property within the class and is equivalent to: $this
. Ex.:
class Foo {
function __construct() {
return self::bar();
}
public function bar() {
return "bar";
}
}
When you run $foo = new Foo()
, the value of the function bar inside the class is returned.
The keyword static
makes a function accessible without having to instantiate the class where it is "hosted". For example:
class Foo {
public static function bar() {
return get_class() . "bar";
}
}
echo Foo::bar();
In this code, Foobar
will be printed. Recalling self
is used to access functions or variables that are within a class and is equivalent to $this
, since static
is used to access functions or variables without needing to call a class.
How much is inherited by other static attributes.