L'opérateur de résolution de portée (aussi appelé Paamayim Nekudotayim) ou,
en termes plus simples, le symbole "double deux-points" (::),
fournit un moyen d'accéder aux membres
une constante,
une propriété statique,
ou une méthode statique
d'une classe ou de l'une de ses classes parentes.
De plus, les propriétés ou méthodes statiques peuvent être surchargées via
la liaison statique tardive.
Lorsque vous référencez ces éléments en dehors de la définition de la
classe, utilisez le nom de la classe.
Il est possible de référencer une classe en utilisant
une variable. La valeur de la variable ne peut être un mot-clé (e.g. self,
parent et static).
Paamayim Nekudotayim pourrait au premier abord sembler être un choix étrange
pour nommer un double deux-points.
Cependant, au moment de l'écriture du Zend Engine 0.5
(qui faisait fonctionner PHP 3), c'est le nom qui a été choisi par l'équipe Zend.
En fait, cela signifie un double deux-points... en hébreu !
Exemple #1 :: en dehors de la définition de la classe
Lorsqu'une classe héritant d'une autre redéfinit une méthode de sa classe parente,
PHP n'appellera pas la méthode de la classe parente. Il appartient à la méthode dérivée
d'appeler la méthode d'origine en cas de besoin. Cela est également valable
pour les définitions des constructeurs et destructeurs,
la surcharge, et les
définitions de méthodes magiques.
Exemple #3 Appel d'une méthode parente
<?php class MyClass { protected function myFunc() { echo "MyClass::myFunc()\n"; } }
class OtherClass extends MyClass { // Surcharge de la définition parente public function myFunc() {
// Mais appel de la méthode parente parent::myFunc(); echo "OtherClass::myFunc()\n"; } }
In PHP, you use the self keyword to access static properties and methods.
The problem is that you can replace $this->method() with self::method() anywhere, regardless if method() is declared static or not. So which one should you use?
Consider this code:
class ParentClass { function test() { self::who(); // will output 'parent' $this->who(); // will output 'child' }
function who() { echo 'parent'; } }
class ChildClass extends ParentClass { function who() { echo 'child'; } }
$obj = new ChildClass(); $obj->test(); In this example, self::who() will always output ‘parent’, while $this->who() will depend on what class the object has.
Now we can see that self refers to the class in which it is called, while $this refers to the class of the current object.
So, you should use self only when $this is not available, or when you don’t want to allow descendant classes to overwrite the current method.
It seems as though you can use more than the class name to reference the static variables, constants, and static functions of a class definition from outside that class using the :: . The language appears to allow you to use the object itself.
For example: class horse { static $props = {'order'=>'mammal'}; } $animal = new horse(); echo $animal::$props['order'];
// yields 'mammal'
This does not appear to be documented but I see it as an important convenience in the language. I would like to see it documented and supported as valid.
If it weren't supported officially, the alternative would seem to be messy, something like this:
Well, a "swiss knife" couple of code lines to call parent method. The only limit is you can't use it with "by reference" parameters.
Main advantage you dont need to know the "actual" signature of your super class, you just need to know which arguments do you need
<?php
class someclass extends some superclass {
// usable for constructors
function __construct($ineedthisone) {
$args=func_get_args();
/* $args will contain any argument passed to __construct.
* Your formal argument doesnt influence the way func_get_args() works
*/
call_user_func_array(array('parent',__FUNCTION__),$args);
}
// but this is not for __construct only
function anyMethod() {
$args=func_get_args();
call_user_func_array(array('parent',__FUNCTION__),$args);
}
// Note: php 5.3.0 will even let you do
function anyMethod() {
//Needs php >=5.3.x
call_user_func_array(array('parent',__FUNCTION__),func_get_args());
}
Little static trick to go around php strict standards ... Function caller founds an object from which it was called, so that static method can alter it, replacement for $this in static function but without strict warnings :)
It's worth noting, that the mentioned variable can also be an object instance. This appears to be the easiest way to refer to a static function as high in the inheritance hierarchy as possible, as seen from the instance. I've encountered some odd behavior while using static::something() inside a non-static method.
See the following example code:
<?php
class FooClass {
public function testSelf() {
return self::t();
}
public function testThis() {
return $this::t();
}
public static function t() {
return 'FooClass';
}
function __toString() {
return 'FooClass';
}
}
class BarClass extends FooClass {
public static function t() {
return 'BarClass';
}
}
$obj = new BarClass();
print_r(Array(
$obj->testSelf(), $obj->testThis(),
));
?>
For the 'late static binding' topic I published a code below, that demonstrates a trick for how to setting variable value in the late class, and print that in the parent (or the parent's parent, etc.) class.
<?php
class cA { /** * Test property for using direct default value */ protected static $item = 'Foo';
/** * Test property for using indirect default value */ protected static $other = 'cA';
public static function method() { print self::$item."\r\n"; // It prints 'Foo' on everyway... :( print self::$other."\r\n"; // We just think that, this one prints 'cA' only, but... :) }
public static function setOther($val) { self::$other = $val; // Set a value in this scope. } }
class cB extends cA { /** * Test property with redefined default value */ protected static $item = 'Bar';
public static function setOther($val) { self::$other = $val; } }
class cC extends cA { /** * Test property with redefined default value */ protected static $item = 'Tango';
public static function method() { print self::$item."\r\n"; // It prints 'Foo' on everyway... :( print self::$other."\r\n"; // We just think that, this one prints 'cA' only, but... :) }
/** * Now we drop redeclaring the setOther() method, use cA with 'self::' just for fun. */ }
class cD extends cA { /** * Test property with redefined default value */ protected static $item = 'Foxtrot';
/** * Now we drop redeclaring all methods to complete this issue. */ }
You use 'self' to access this class, 'parent' - to access parent class, and what will you do to access a parent of the parent? Or to access the very root class of deep class hierarchy? The answer is to use classnames. That'll work just like 'parent'. Here's an example to explain what I mean. Following code
<?php class A { protected $x = 'A'; public function f() { return '['.$this->x.']'; } }
class B extends A { protected $x = 'B'; public function f() { return '{'.$this->x.'}'; } }
class C extends B { protected $x = 'C'; public function f() { return '('.$this->x.')'.parent::f().B::f().A::f(); } }