downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Statique> <Visibilité
Last updated: Fri, 14 Aug 2009

view this page in

L'opérateur de résolution de portée (::)

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 statiques ou constants ainsi qu'aux éléments redéfinis par la classe.

Lorsque vous référencez ces éléments en dehors de la définition de la classe, utilisez le nom de la classe.

Depuis PHP 5.3.0, 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 peut sembler un choix étrange pour un double deux points. Cependant, au moment de l'écriture du Zend Engine 0.5 (fourni avec PHP 3), c'est le nom choisi par le groupe Zend. En fait, cela signifie un double deux points... en hébreu !

Exemple #1 :: en dehors de la définition de la classe

<?php
class MyClass {
    const 
CONST_VALUE 'Une valeur constante';
}

$classname 'MyClass';
echo 
$classname::CONST_VALUE// Depuis PHP 5.3.0

echo MyClass::CONST_VALUE;
?>

Deux mots-clé spéciaux, self et parent, sont utilisés pour accéder aux membres ou aux méthodes depuis la définition de la classe.

Exemple #2 :: depuis la définition de la classe

<?php
class OtherClass extends MyClass
{
    public static 
$my_static 'variable statique';

    public static function 
doubleColon() {
        echo 
parent::CONST_VALUE "\n";
        echo 
self::$my_static "\n";
    }
}

$classname 'OtherClass';
echo 
$classname::doubleColon(); // Depuis PHP 5.3.0

OtherClass::doubleColon();
?>

Lorsqu'une classe étendue redéfinit une méthode de la classe parente, PHP n'appellera pas la méthode d'origine. 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, les surcharges et les 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
{
    
// Dépassement de la définition parent
    
public function myFunc() {

      
// Mais appel de la fonction parent
      
parent::myFunc();
      echo 
"OtherClass::myFunc()\n";
  }
}

$class = new OtherClass();
$class->myFunc();
?>


Statique> <Visibilité
Last updated: Fri, 14 Aug 2009
 
add a note add a note User Contributed Notes
L'opérateur de résolution de portée (::)
wouter at interpotential dot com
09-Nov-2009 04:24
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(),
));
?>

which outputs:

<pre>
Array
(
    [0] => FooClass
    [1] => BarClass
)
</pre>

As you can see, __toString has no effect on any of this. Just in case you were wondering if perhaps this was the way it's done.
giovanni at gargani dot it
02-Jun-2009 03:38
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());
 }

}
?>
erwinhaantjes at interurbia dot nl
11-Apr-2009 11:36
Here is a nice function i like to share with you because it so nice and handy feature. I wanted to get rid off the parent::methodName(myVar1, myVar2, myVar3 etc.....) construction so i wrote a shortcut to this notation. The only thing you have to do is calling $this->inherited() (or parent::inherited()) to call the same function with the same parameters in the parent class. It is also possible to specify other parameters if you like. You can also use this inside constructors/destructors.

To use this example, put it in the base class of all objects.You also need the simple function below to test if an array is valid (from a self-build library) but you are free to change it to your own needs ;-):

<?php
function suIsValidArray( &$a, &$iCount = null )
{
 
$iCount = 0;
  return (
is_array( $a ) and (( $iCount = @count( $a )) > 0 )); 
}
?>

The inherited function/construction is familiar language construction in Object Pascal/Delphi (wonderful language). If you have any comments, please enter a note.  

Here it is:

<?php
   
public function inherited()
    {
      
// Use DEBUG backtrace to trace caller function
     
$bt = debug_backtrace();
     
$bt = $bt[ 1 ]; // List is in reversed order, 0 reffers to this function so get previous one
     
     
if( !suIsValidArray( $bt ))
      { return; }
     
     
$sClassName = $bt["class"];
     
$sParentClassName = get_parent_class( $sClassName );
      if( empty(
$sClassName ) or empty( $sParentClassName ) or $sParentClassName == $sClassName )
      { return; }
     
     
$sFuncName = $bt["function"];
      
// constructor or destructor called (old fashion way)?
     
if( ( $bIsConstruct = ( $sFuncName == $sClassName )) or ( $sFuncName == "_".$sClassName ) )
      {
         
// get parent constructor/destructor
         
$sFuncName = (( !$bIsConstruct ) ? "_" : "" ).$sParentClassName;
        if(
$sFuncName == (( !$bIsConstruct ) ? "_" : "" ).$sClassName )
        { return; }
      }     
      
      if(
method_exists( $sParentClassName, $sFuncName ))
       {
            
// If there are parameters specified, use these
        
$args = func_get_args();
         if( !
suIsValidArray( $args ))
          {
$args = &$bt["args"]; } // otherwise use previous function parameters if any
     
          
$iCount = 0;
          
$sArgs = "";
        if(
suIsValidArray( $args, $iCount ))
         {
           for(
$i = 0; $i < $iCount; $i++ )
            {
$sArgs.="&$"."args[$i]".( $i < ($iCount-1) ? "," : "" ); }
         }   
        
        
// Simple, evaluate it, this is possible because it is done inside the class itself, and
         // the parent class is already created because is a part of the class structure, no worry, no scope issues  
       
eval( "$"."result = $sParentClassName::".$sFuncName."($sArgs);" );
        return @
$result;
       } 
    }
?>
csaba dot dobai at php-sparcle dot com
03-Feb-2009 06:54
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.
     */
}

cB::setOther('cB'); // It's cB::method()!
cB::method(); // It's cA::method()!
cC::setOther('cC'); // It's cA::method()!
cC::method(); // It's cC::method()!
cD::setOther('cD'); // It's cA::method()!
cD::method(); // It's cA::method()!

/**
 * Results: ->
 * Foo
 * cB
 * Tango
 * cC
 * Foo
 * cD
 *
 * What the hell?! :)
 */

?>
luka8088 at gmail dot com
25-Jan-2009 12:15
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 :)

<?php

error_reporting
(E_ALL + E_STRICT);

function
caller () {
 
$backtrace = debug_backtrace();
 
$object = isset($backtrace[0]['object']) ? $backtrace[0]['object'] : null;
 
$k = 1;
       
  while (isset(
$backtrace[$k]) && (!isset($backtrace[$k]['object']) || $object === $backtrace[$k]['object']))
   
$k++;

  return isset(
$backtrace[$k]['object']) ? $backtrace[$k]['object'] : null;
}

class
a {

  public
$data = 'Empty';
 
  function
set_data () {
   
b::set();
  }

}

class
b {

  static function
set () {
   
// $this->data = 'Data from B !';
    // using this in static function throws a warning ...
   
caller()->data = 'Data from B !';
  }

}

$a = new a();
$a->set_data();
echo
$a->data;

?>

Outputs: Data from B !

No warnings or errors !
barss dot dev at gmail dot com
01-Jul-2008 12:47
Nice trick with scope resolution
<?php
   
class A
   
{
        public function
TestFunc()
        {
            return
$this->test;
        }
    }

    class
B
   
{
        public
$test;

        public function
__construct()
        {
           
$this->test = "Nice trick";
        }

        public function
GetTest()
        {
            return
A::TestFunc();
        }
    }

   
$b = new B;
    echo
$b->GetTest();
?>

will output

Nice trick
richard at richard-sumilang dot com
24-Mar-2008 03:27
Actually, for people not using PHP 5.3 yet you should try avoiding the use of a eval() at all costs! THere are too many security risks and dirty code that come from using eval. If you want to call a static method from a class then you can use call_user_func() instead which is much safer.

Example:

clas Foo{

  public static method Bar(){
    echo "Hello world!";
  }

}

And to execute that with call_user_func you would do the following:

call_user_func(array('Foo', 'bar') [, $params] );

Thanks,
- Richard S.
thomas dot sahlin at gmail dot com
27-Dec-2007 01:15
If you need to reference a class using a variable (prior to PHP 5.3.0) you may use something like this:

<?php
$className
= 'MyClass';

eval(
'$foo = ' . $className . '::myFunction();');

var_dump($foo);
?>
Anonymous
17-Oct-2007 12:41
There's no surprize here. These resolution rules are identical in C++ and Java, where static fields (and methods) are not separately instanciated within the inheritance tree for the new derived classes.

This is per design. If you want per-class static fields,you have to overload each static field (or method) to assign them a new value that will hide the inherited static field (or method). The "$self::" scope in a static method does not refer to the object or class from which the method is called, as it is statically compiled and resolved within the class declaring the method.

In other words "$self::" is just needed to specify the field declared in the defining class, instead of a homonym variable (or function) in the local scope which may hide it. This is exactly similar to the "this." scope specifier (used with the "." operator here) used in C++ (or Java).

The same can be said about the semantic of "$super::" used within a static method (similar to "super." scope specifier used in C++ or Java).

Neither PHP, nor C++, have a way to work on instanciated class objects as if they were objects of the first grade (that's why C++ and Java are making distinctions between classes and interfaces).

On the opposite Javascript/ECMAScript defines classes like standard objects with a "prototype" field for the fields (or methods) to inherit instead of duplicating them with the new operator. When working in PHP, forget Javascript, think like in C++ and Java.
mongoose643 at gmail dot com
13-Feb-2007 09:11
This is a solution for those that still need to write code compatible with php 4 but would like to use the flexibility of static variables. PHP 4 does not support static variables within the class scope but it does support them within the scope of class methods. The following is a bit of a workaround to store data in static mode in php 4.

Note: This code also works in PHP 5.

(Tested on version 4.3.1+)

The tricky part is when using when arrays you have to do a bit of fancy coding to get or set individual elements in the array. The example code below should show you the basics of it though.

<?php

class StaticSample
{
   
//Copyright Michael White (www.crestidg.com) 2007
    //You may use and modify this code but please keep this short copyright notice in tact.
    //If you modify the code you may comment the changes you make and append your own copyright
    //notice to mine. This code is not to be redistributed individually for sale but please use it as part
    //of your projects and applications - free or non-free.
   
   
    //Static workaround for php4 - even works with arrays - the trick is accessing the arrays.
    //I used the format s_varname for my methods that employ this workaround. That keeps it
    //similar to working with actual variables as much as possible.
    //The s_ prefix immediately identifies it as a static variable workaround method while
    //I'm looking thorugh my code.
   
function &s_foo($value=null, $remove=null)
    {
        static
$s_var;    //Declare the static variable.    The name here doesn't matter - only the name of the method matters.
       
       
if($remove)
        {
            if(
is_array($value))
            {
                if(
is_array($s_var))
                {
                    foreach(
$value as $key => $data)
                    {
                        unset(
$s_var[$key]);
                    }
                }
            }
            else
            {
               
//You can't just use unset() here because the static state of the variable will bring back the value next time you call the method.
               
$s_var = null;
                unset(
$s_var);
            }
           
//Make sure that you don't set the value over again.
           
$value = null;
        }
        if(
$value)
        {
            if(
is_array($value))
            {
                if(
is_array($s_var))
                {
                   
//$s_var = array_merge($s_var, $value);        //Doesn't overwrite values. This adds them - a property of the array_merge() function.
                   
foreach($value as $key => $data)
                    {
                       
$s_var[$key] = $data;    //Overwrites values.
                   
}
                }
                else
                {
                   
$s_var = $value;
                }
            }
            else
            {
               
$s_var = $value;
            }
        }
       
        return
$s_var;
    }
}

echo
"Working with non-array values.<br>";
echo
"Before Setting: ".StaticSample::s_foo();
echo
"<br>";
echo
"While Setting: ".StaticSample::s_foo("VALUE HERE");
echo
"<br>";
echo
"After Setting: ".StaticSample::s_foo();
echo
"<br>";
echo
"While Removing: ".StaticSample::s_foo(null, 1);
echo
"<br>";
echo
"After Removing: ".StaticSample::s_foo();
echo
"<hr>";
echo
"Working with array values<br>";
$array = array(0=>"cat", 1=>"dog", 2=>"monkey");
echo
"Set an array value: ";
print_r(StaticSample::s_foo($array));
echo
"<br>";

//Here you need to get all the values in the array then sort through or choose the one(s) you want.
$all_elements = StaticSample::s_foo();
$middle_element = $all_elements[1];
echo
"The middle element: ".$middle_element;
echo
"<br>";

$changed_array = array(1=>"big dog", 3=>"bat", "bird"=>"flamingo");
echo
"Changing the value: ";
print_r(StaticSample::s_foo($changed_array));
echo
"<br>";

//All you have to do here is create an array with the keys you want to erase in it.
//If you want to erase all keys then don't pass any array to the method.
$element_to_erase = array(3=>null);
echo
"Erasing the fourth element: ";
$elements_left = StaticSample::s_foo($element_to_erase, 1);
print_r($elements_left);
echo
"<br>";
echo
"Enjoy!";

?>
developit at mail dot ru
27-Jan-2006 12:57
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();
    }
}

$a = new A();
$b = new B();
$c = new C();

print
$a->f().'<br/>';
print
$b->f().'<br/>';
print
$c->f().'<br/>';
?>

will output

[A] -- {B} -- (C){C}{C}[C]
Kristof Coomans
25-Nov-2005 12:08
In response to ian at [first name]henderson dot org:

(related bogus bug report: http://bugs.php.net/bug.php?id=26930)

The functionality you've expected maybe will be possible in PHP6, probably by using the static keyword in conjunction with the scope resolution parameter. You can read more about this in the minutes of the PHP developers meeting at 11 and 12 november in Paris: http://www.php.net/~derick/meeting-notes.html point 5.4: Late static binding using "this" without "$" (or perhaps with a different name)
HuugjeWeg
29-Apr-2005 02:58
In response to ian at [first name]henderson dot org:

You are not allowed to redefine static methods, see
http://www.php.net/manual/en/language.oop5.static.php

And in response to thenewparadigm at hotmail dot com: the behaviour you describe seems appropriate for *classes* with static variables, see "Using static variables" on http://nl2.php.net/static
thenewparadigm at hotmail dot com
05-Mar-2005 06:43
There is also a quirk with using the scope resolution operator on static class variables.  Below is an example using a highly modified version of Ian's code:

<?php

class ExampleSuperclass
{
   static
$className;

   static function
showClassName() {
      echo
self::$className . "\n";
   }
}

class
ExampleSubclassOne extends ExampleSuperclass
{
   static function
setClassName()
   {
      
self::$className = "subclassOne";
   }
}

class
ExampleSubclassTwo extends ExampleSuperClass
{
   static function
setClassName()
   {
     
self::$className = "subclassTwo";
   }
}

// setting variables for each class
ExampleSubclassOne::setClassName();
ExampleSubclassTwo::setClassName();

ExampleSubclassOne::showClassName();  // output is "subclassTwo"!

// more output:

echo ExampleSubclassOne::$className . "\n"; // output is "subclassTwo"!
echo ExampleSubclassTwo::$className . "\n"; // output is "subclassTwo"
echo ExampleSuperclass::$className . "\n"; // output is "subclassTwo"!

?>

appearantly, any static variables defined in a superclass are directly referenced in subclasses,
and all changes are visible throughout the class heirarchy.  care must be taken when using static
class variables.
ian at [first name]henderson dot org
01-Feb-2005 07:43
Please note that methods called by the scope resolution operator which are defined by a superclass of the first operand are called in the scope of the SUPERCLASS.  For example,

<?php

class ExampleSuperclass
{
    static function
classType()
    {
        return
"superclass";
    }

    static function
doSomething()
    {
        echo
"doing something with " . self::classType();
    }
}

class
ExampleClass extends ExampleSuperclass
{
    static function
classType()
    {
        return
"subclass";
    }
}

ExampleClass::doSomething();
// output is "doing something with superclass"!

?>

This can be surprising (it surprised me!) when coming from other object-oriented languages, which would output "doing something with subclass" in this case.

Statique> <Visibilité
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites