Etki Alanı Çözünürlük İşleci (::)

Etki Alanı Çözünürlük İşleci (Paamayim Nekudotayim de denirmiş), basitçe bir çift iki nokta iminden ibaret olup (buna "iki nokta çifti" de denebilir), bir sınıfın veya ebeveyninin statik, sabit özellik veya static yöntemine erişilebilmesini sağlayan bir simgedir. Ayrıca, statik özellik ve yöntemler statik içselleştirim aracılığıyla geçersiz kılınabilir.

Bu öğelere sınıf tanımının dışından erişirken bu işlecin solunda sınıfın ismini kullanın.

Bu işlecin solunda sınıf ismi yerine bir değişken kullanmak da mümkündür. Değişken ismi, (self, parent, static gibi) bir anahtar sözcük olmamalıdır.

Paamayim Nekudotayim ilk bakışta iki nokta üst üste için garip bir adlandırma gibi gözükebilir. Ancak, Zend Engine 0.5 (php 3'ün kalbidir) yazıldığı esnada Zend ekibinin kullanmak istediği terim bu oldu. Esasında bu iki sözcük de iki nokta çifti anlamına gelmektedir - İbranice'de!

Örnek 1 - Sınıf tanımı dışında ::

<?php
class Sınıfım {
const
SABİT_DEĞER = 'Bir sabit değer';
}

echo
Sınıfım::SABİT_DEĞER;
?>

Bir sınıfın özellik ve yöntemlerine bu işleç sayesinde sınıfın içinden erişilmek istendiğinde işlecin solunda self, parent veya static anahtar sözcüğü kullanılır.

Örnek 2 - Sınıf tanımı içinde ::

<?php
class BaşkaSınıf extends Sınıfım
{
public static
$statik = 'Bir statik değişken';

public static function
ikiNoktaÇifti() {
echo
parent::SABİT_DEĞER . "\n";
echo
self::$statik . "\n";
}
}

$sınıfadı = 'BaşkaSınıf';
$sınıfadı::ikiNoktaÇifti();

BaşkaSınıf::ikiNoktaÇifti();
?>

Çocuk sınıf ebeveyninin yöntem tanımlarından bazılarını geçersiz kılarsa PHP bu yöntemlerin ebeveyn tanımlarına çağrı yapmaz. Ebeveynin yönteminin çağrılıp çağrılmayacağı çocuk sınıfın inisiyatifindedir. Bu durum aynı zamanda Kurucular ve Yıkıcılar, Aşırı Yükleme ve Sihirli yöntem tanımları için de geçerlidir.

Örnek 3 - Ebeveynin yöntemini çağırmak

<?php
class Sınıfım
{
protected function
işlevim() {
echo
"Sınıfım::işlevim()\n";
}
}

class
BaşkaSınıf extends Sınıfım
{
// Ebeveynin tanımını geçersiz kılalım
public function işlevim()
{
// Ama hala ebeveynin işlevini çağırabiliriz
parent::işlevim();
echo
"BaşkaSınıf::işlevim()\n";
}
}

$class = new BaşkaSınıf();
$class->işlevim();
?>

Ayrıca, statik çağrı hilesi örneklerine de bakınız.

add a note add a note

User Contributed Notes 11 notes

up
224
Theriault
14 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
103
1naveengiri at gmail dot com
7 years 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
33
guy at syntheticwebapps dot com
11 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
22
jasverix at NOSPAM dot gmail dot com
11 years ago
Just found out that using the class name may also work to call similar function of anchestor class.

<?php

class Anchestor {
  
   public
$Prefix = '';

   private
$_string 'Bar';
    public function
Foo() {
        return
$this->Prefix.$this->_string;
    }
}

class
MyParent extends Anchestor {
    public function
Foo() {
        
$this->Prefix = null;
        return
parent::Foo().'Baz';
    }
}

class
Child extends MyParent {
    public function
Foo() {
       
$this->Prefix = 'Foo';
        return
Anchestor::Foo();
    }
}

$c = new Child();
echo
$c->Foo(); //return FooBar, because Prefix, as in Anchestor::Foo()

?>

The Child class calls at Anchestor::Foo(), and therefore MyParent::Foo() is never run.
up
12
giovanni at gargani dot it
15 years ago
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());
}

}
?>
up
10
remy dot damour at ----no-spam---laposte dot net
14 years ago
As of php 5.3.0, you can use 'static' as scope value as in below example (add flexibility to inheritance mechanism compared to 'self' keyword...)

<?php

class A {
    const
C = 'constA';
    public function
m() {
        echo static::
C;
    }
}

class
B extends A {
    const
C = 'constB';
}

$b = new B();
$b->m();

// output: constB
?>
up
-1
gazianis2200 at gmail dot com
10 months ago
<?php
/**
*access a constant from outside a class
*/
class Foo{
    public const
A = "Constant A";
}
echo
Foo::A;
echo
"\n";

/**
*access a constant within its own class
*/

class Bar{
    public const
A = "Constant A";
    public function
abc(){
        echo
self::A;
        echo
"\n";
    }
}

$obj = new Bar;
$obj->abc();

/**
*access a constant within her child class
*/

class Baz extends Bar{
    public function
abc(){
        echo
parent::A;
    }
}
$obj = new Baz;
$obj->abc();

//Static property and static method also follows this principle.
up
2
wouter at interpotential dot com
15 years ago
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.
up
2
luka8088 at gmail dot com
15 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
0
csaba dot dobai at php-sparcle dot com
15 years ago
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?! :)
*/

?>
up
0
developit at mail dot ru
18 years ago
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]
To Top