Scope Resolution Operator (::) and Static Keyword PDF Print E-mail
User Rating: / 0
PoorBest 
Monday, 16 March 2009
HTML clipboard

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 members 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;
?>
 

Two special keywords self and parent are used to access members 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';
echo
$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();
?>
 
 
 
HTML clipboard

Static Keyword

Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).

For compatibility with PHP 4, if no visibility declaration is used, then the member or method will be treated as if it was declared as public.

Because static methods are callable without an instance of the object created, the pseudo variable $this is not available inside the method declared as static.

Static properties cannot be accessed through the object using the arrow operator ->.

Calling non-static methods statically generates an E_STRICT level warning.

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).

Example #1 Static member example
<?php
class Foo
{
  
public static $my_static = 'foo';

  
public function staticValue() {
       return
self::$my_static;
   }
}

class
Bar extends Foo
{
  
public function fooStatic() {
       return
parent::$my_static;
   }
}


print
Foo::$my_static . "\n";

$foo = new Foo();
print
$foo->staticValue() . "\n";
print
$foo->my_static . "\n";      // Undefined "Property" my_static

print $foo::$my_static . "\n";
$classname = 'Foo';
print
$classname::$my_static . "\n"; // As of PHP 5.3.0

print Bar::$my_static . "\n";
$bar = new Bar();
print
$bar->fooStatic() . "\n";
?>
 
Example #2 Static method example
<?php
class Foo {
  
public static function aStaticMethod() {
      
// ...
  
}
}

Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
?>
 
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials