::

"Paamayim Nekudotayim" или просто "двойное двоеточие". Используя эту лексему, программист может обращаться к константам, статическим или перегруженным свойствам или методам класса.

При обращении к этим элементам извне класса, программист должен использовать имя этого класса.

"Paamayim Nekudotayim" на первый взгляд может показаться странным словосочетанием для обозначения двойного двоеточия. Однако, во время создания Zend Engine версии 0.5 (который входил в PHP3), Andi и Zeev выбрали именно это обозначение. "Paamayim Nekudotayim" действительно значит "двойное двоеточие". На иврите. Просто это обозначение не менялось ни разу в течение всего времени разработки PHP.

Пример 19-11. Использование :: вне объявления класса

<?php
class MyClass {
  const
CONST_VALUE = 'Значение константы';
}
echo
MyClass::CONST_VALUE;
?>

Для обращения к свойствам и методам в объявлении класса используются ключевые слова self и parent.

Пример 19-12. Использование :: в объявлении класса

<?php
class OtherClass extends MyClass {
  public static
$my_static = 'статическая переменная';

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

OtherClass::doubleColon();
?>

Когда дочерний класс перегружает методы, объявленные в классе-родителе, PHP не будет осуществлять автоматический вызов методов, принадлежащих классу-родителю. Этот функционал возлагается на метод, перегружаемый в дочернем классе. Данное правило распространяется на конструкторы и деструкторы, перегруженные и "волшебные" методы.

Пример 19-13. Обращение к методу в родительском классе

<?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();
?>


::
developit at mail dot ru
27-Jan-2006 03: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 03: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: ~derick/meeting-notes.html point 5.4: Late static binding using "this" without "$" (or perhaps with a different name)
zeldorblat at gmail dot com
07-Sep-2005 09:47
The alternative to john at johnjosephbachir dot org's suggestion without using eval():

call_user_func(array($classname, 'a_static_function'));
john at johnjosephbachir dot org
30-Aug-2005 03:23
A way to achieve Doug's last example

<?php
//this doesn't work
$classname::a_static_function();
?>

is to use eval():

<?php
//this works
$string = $classname.'::a_static_function();';
eval(
$sting);
?>

Remember to include a semicolon inside the string passed to eval().
doug at douglassdavis dot com
29-Aug-2005 07:52
Just a note that while this works for new objects:

<?php

$obj
= new $classname();

?>

and this works to call a function:

<?php

$functionname
();

?>

this does not work to call static functions, you will get a parse error:

<?php

$classname
::a_static_function();

?>
HuugjeWeg
29-Apr-2005 05:58
In response to ian at [first name]henderson dot org:

You are not allowed to redefine static methods, see
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 09: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
31-Jan-2005 10: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.
a dot naumovic at e-motion dot co dot yu
19-Oct-2004 03:42
One additional note about Pa'ama'im Nekudota'im: a suffix ayim is for dual, so the Hebrew language has singular, plural and dual. It's a perfect way to express existence of two  entities of something.
seec77 at zahav dot net dot il
13-Oct-2004 03:01
Just a little fun note about the term "Pa'ama'im Nekudota'im". As a person who originated, and still lives in Israel (land of the Hebrew language), I wanted to explain to everyone what "Pa'ama'im Nekudota'im" means.

"Nekudota'im", is the Hebrew term for a colon.
"Nekudota'im" is a combination of two words... "Neukda", which means "dot"... and "Shta'im" (female form of "Shna'im"), which means "two". Thus, "Nekudota'im" means two dots, and that's why in Hebrew it is used for the colon sign.

"Pa'ama'im", is also a combination of two words. "Pa'am", which means "a time", and again, the postfix originating from "Shtai'm". So "Pa'ama'im" means "two times", or "twice".

We can conclude this, by saying that the expression "Pa'ama'im Nekudota'im" means two times a pair of dots, or a pair of colons, which makes the expression totally legable for calling such an operator.
:)

<VisibilityStatic Keyword>
 Last updated: Tue, 15 Nov 2005