PHP 7.0.26 Released

Scope Resolution Operator (::)

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

When referencing these items from outside the class definition, use the name of the class.

As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).

Paamayim Nekudotayim would, at first, seem like a strange choice for naming a double-colon. However, while writing the Zend Engine 0.5 (which powers PHP 3), that's what the Zend team decided to call it. It actually does mean double-colon - in Hebrew!

Example #1 :: from outside the class definition

<?php
class MyClass {
    const 
CONST_VALUE 'A constant value';
}

$classname 'MyClass';
echo 
$classname::CONST_VALUE// As of PHP 5.3.0

echo MyClass::CONST_VALUE;
?>

Three special keywords self, parent and static are used to access properties or methods from inside the class definition.

Example #2 :: from inside the class definition

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

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

$classname 'OtherClass';
$classname::doubleColon(); // As of PHP 5.3.0

OtherClass::doubleColon();
?>

When an extending class overrides the parents definition of a method, PHP will not call the parent's method. It's up to the extended class on whether or not the parent's method is called. This also applies to Constructors and Destructors, Overloading, and Magic method definitions.

Example #3 Calling a parent's method

<?php
class MyClass
{
    protected function 
myFunc() {
        echo 
"MyClass::myFunc()\n";
    }
}

class 
OtherClass extends MyClass
{
    
// Override parent's definition
    
public function myFunc()
    {
        
// But still call the parent function
        
parent::myFunc();
        echo 
"OtherClass::myFunc()\n";
    }
}

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

See also some examples of static call trickery.

add a note add a note

User Contributed Notes 12 notes

up
66
Theriault
7 years ago
A class constant, class property (static), and class function (static) can all share the same name and be accessed using the double-colon.

<?php

class A {

    public static
$B = '1'; # Static class variable.

   
const B = '2'; # Class constant.
   
   
public static function B() { # Static class function.
       
return '3';
    }
   
}

echo
A::$B . A::B . A::B(); # Outputs: 123
?>
up
9
1naveengiri at gmail dot com
6 months ago
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.
up
15
guy at syntheticwebapps dot com
4 years ago
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:

$animalClass = get_class($animal);
echo $animalClass::$props['order'];
up
6
luka8088 at gmail dot com
8 years ago
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 !
up
6
remy dot damour at ----no-spam---laposte dot net
7 years ago
As of php 5.3.0, you can use 'static' as scope value as in below example (add flexibieity to m-voteu0meid="ism dotpdeclaity't it'self keythi) r />
< r />A const = cA B !' ...
        ese 'strd">::
  &nb} ...
class
B A const = cB B !'$a = new B$a->cB r />?>
up
2 6
giovannngiri arg="ioste it net
8 years ago
clknif"dacouexam Ase">Thi3.0, docl r />
< r />class ssupe>r />class Construc r />Constset ...
&nbcho
...
&nbcho
Const.r /> &nb ...
&nb* Y dam &nb*/ ...
&nbcho
$a $a Const elf r /> ...
&nbcho
&nbcho $a $a ...
&nbcho
?=php x ...
&nbcho
$a $a ?>
up
2 6
b8 ybapps dev8088 at gmail dot com
8 years ago
<?php
   
    myFuncreturn $a->;
&nbr />&nbr />&nbr />  }
    }

  &nbr />class
        puburn ;        pubtic function Constset $a->= ;
&nbr />&nbr />&nbr />  }        pubtic function
      &nbo() {
          &nbrd">return
A::myFunc;
&nbr />&nbr />&nbr />  }
    }

  &nbrata
$a = new B;
&nbr />&nbr />echo
$a->();
?>

up
66
willeamourinleapo-conspecail dot com
8 years ago
sode anhavile, o; wh // usse 'strdsy, sometthodom insadd n-hat static me. ...
< r />class self ...
      &nbrd">return
self::  &nb} ...$a ...
      &nbrd">return
$a::  &nb} ... ...
      &nbrd">return
  &nb} ... ...
      &nbrd">return
  &nb} ...
class
...
      &nbrd">return
  &nb} ...= new ">$a->self">$a->$a? ...Outp ...pret">? ...  &nb[0] der'= FooC />c ...
  &nb[1] der'= laOtherC .../pret">? ...
up
66
csabaapps dobangiriote--norclemail dot com
8 years ago
Coad Noaotic trent h belke tte"static varitope vance in thtn the cants, p="st 3), ence in ‘pa3( />nce bsp;pare bsp;pa, etc.) the colon.

<?php

class () {
   
  &nr />&nb*/?php
   
= '3';
  &nb() {
   
  &nr />&nb*/?php
   
= ;
  &nb() {
    pubsp; static function
  &nbo() {
      &nbs="st urn
self::=         self::= ]))
   
self::=     B
  &nr />&nb* Tgt;lass proped() wredss ded ss="deftope v() {
  &nr />&nb*/?php
   
= ;
  &nb() {
    pubsp; static function
  &nbo() {
      &nburn
self::=     }
}

class
() {
   
  &nr />&nb*/?php
   
= '3';
  &nb() {
    pubsp; static function
  &nbo() {
      &nbs="st urn
self::=         self::= ]))
   
  &nr />&nb* r />Nowdss wredspanrs using ttO whe()atic me, can cAed() w'le, se' juse ent ticg ...
  &nr />&nb*/?php
  &nr />&nb* Tgt;lass proped() wredss ded ss="deftope v() {
  &nr />&nb*/?php
   
= '3';
  &nb() {
   
::(); ::(); ::(); ::(); ::(); ::(); ?>]))?new
up
0 15
jasverixgiriNOSPAMmail at gmail dot com
4 years ago

<?php

class () {
&nsp; &nb() {
&nsp;   pubtic
= '3';&nsp; &nbsrivhtn tic ;
    pubtic function
      &nbrd">return $a->= $a->$a->= null;
      &nbrd">return
::= ;
    }
}

class
() {
    pubtic function
      &nburn $a->= '3';
      &nbrd">return
::;
    }
}
= new ;
echo
->(); retFoolaO, becacan Pr" ix,alue aslgengt;o>::Foo()?php?n...cl to sgirilgengt;o>::Foo()ants, wheefse mMynds Pa::Foo()this ever ricg n>
up
0 15
developsee t at gmail ru com
4 years ago

<?php
class () {
  &nbbsp;protebcho
= ;
    pubtic function
  &nbo() {
      &nbrd">return
= $a->= ;
    }
}

class
B
= ;
    pubtic function
  &nbo() {
      &nbrd">return
= $a->= ;
    }
}

class
B
= ;
    pubtic function
  &nbo() {
      &nbrd">return
= $a->= = ::= ::= A::;
    }
}
$a = new A();
$a = new B= new B$a->= br/d">- B !';$a->= br/d">- B !';->= br/d">- B !';?n...
up
-4 15
m
4 years ago
clias scion iu his supportb inmed() nce in ias sc Ase />cland metth: . foto a/ ushisa bia3assaw"kekarn arolke tse mlt">dhis in stamis his of 4olon.
iextvid/ma eledocu>Tnce in arrayth: . low examhis s in beshould"sh be.0, in basicclassitb iougholon.

<?php

class () {
   
  &nb//IAsy d misify enamhis syou .pa ss="com enamhhge=essyou .pko ts, hge s, y damd">bcopyright ...
  &nb//use"cvaio minelled. This shis is arsbrerediss=" onclaiextvid/malpeent sal s on exaasgucan in as t ...
  &nb// Asy dampron obs ts, hge puasolus - freeingsd n-freeg ...
  &nbs...
  &nbs...
  &nb//Sin stw"kekarn aroent lt;4 - even "kekued() warrayit- enamtic trhisaccesss using arrayig ...
  &nb//Iucandence form, es_ticass nent mycand metid), ee exoy en $t"kekarn arlledatukeeps it ...
  &nb//simianratem"keks usw() waIt acatic variseas mu whas 9;s possg ...
  &nb//: . s_mpr" ixti="cdis="lpeidcomufies in as ablic static variw"kekarn aroand me, o; w ...
  &nb//I'm looks usinorugh mychis g ...
   
nullnull  &nb: . ass nwhee his c't attleam- elf ot . ass nass oe and me,attleaig ...
  &nbr />  &nbs...
  &nbr />   
$a $a (ic ;
&nbr />&nbr />&nbr />&nbr />&nbr />&nbr />&nbr />&nbr />&nbr />  }
  &nbr />&nbr />&nbr />&nbr />&nbr />&nbr />  }
  &nbr />&nbr />&nbr />&nbr />  }
  &nbr />&nbr />&nbr />&nbr />&nbelsw ...
          &nbo() {
               
= null;
              &nbunlt"rurn
;
&nbr />&nbr />&nbr />&nbr />&nbr />  }
  &nbr />&nbr />&nbr />&nbr /> 
oe tie vbover agaicg ...
           
= null;
        }
  &nbr />&nbr />&nbifrurn
      &nbo() {
          &nbifrurn
          &nbo() {
              &nbifrurn
              &nbo() {
              &nbr />&nbr /> 
(ic = (ic   &nbr />          &nbr />&nbr />  = ;
                }
  &nbr />&nbr />&nbr />&nbr />  }
  &nbr />&nbr />&nbr />&nbr />&nbelsw ...
          &nbo() {
               
= ;
            }
  &nbr />&nbr />  }
  &nbr />&nbr />&nbd">;
      &nbrd">return
;
    }
}

echo
brsr'="B !';
echo
= A::;
echo
brsr'="B !';
echo
= A::;
echo
brsr'="B !';
echo
= A::;
echo
brsr'="B !';
echo
= A::null;
echo
brsr'="B !';
echo
= A::;
echo
hrsr'="B !';
echo
brsr'="B !';;
echo
;= A::;
echo
brsr'="B !';;Tata = A::;= ;
echo
= ;
echo
brsr'="B !';;;
echo
;= A::;
echo
brsr'="B !';;null;
echo
;= A::;= ;
echo
brsr'="B !';
echo
;;?new
add as isean>mo/ >ara;< e func> 15 >e func>