php范围解析操作符(::)
By admin
- 6 minutes read - 1260 words范围解析操作符(::) 范围解析操作符(也可称作 Paamayim Nekudotayim)或者更简单地说是一对冒号,可以用于访问静态成员、方法和常量,还可以用于覆盖类中的成员和方法。
当在类的外部访问这些静态成员、方法和常量时,必须使用类的名字。
把 Paamayim Nekudotayim 选作该操作符的名字似乎有些奇怪。然而,这是 Zend 开发小组在写 Zend Engine 0.5 (被用于 PHP 3 中)时所作出的决定。事实上这个词在希伯莱文就是双冒号的意思。
Example#1 在类的外部使用 :: 操作符
self 和 parent 这两个特殊的关键字是用于在类的内部对成员或方法进行访问的。
Example#2 :: from inside the class definition
当一个子类覆盖其父类中的方法时,PHP 不会再执行父类中已被覆盖的方法,直到子类中调用这些方法为止。这种机制也作用于 构造函数和析构函数、重载 及 魔术 函数。
Example#3 调用父类的方法
myFunc();
?>
Static Keyword 访问控制 ——————————————————————————– Last updated: Sun, 25 Nov 2007
add a note User Contributed Notes 范围解析操作符(::) barss dot dev at gmail dot com 01-Jul-2008 03:47 Nice trick with scope resolution 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 07: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 04:15 If you need to reference a class using a variable (prior to PHP 5.3.0) you may use something like this:
Anonymous 16-Oct-2007 03: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 12: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.
$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. “; echo “Before Setting: “.StaticSample::s_foo(); echo “ “; echo “While Setting: “.StaticSample::s_foo(“VALUE HERE”); echo “ “; echo “After Setting: “.StaticSample::s_foo(); echo “ “; echo “While Removing: “.StaticSample::s_foo(null, 1); echo “ “; echo “After Removing: “.StaticSample::s_foo(); echo “
“; echo “Working with array values “; $array = array(0=>”cat”, 1=>”dog”, 2=>”monkey”); echo “Set an array value: “; print_r(StaticSample::s_foo($array)); echo “ “;
//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 “ “;
$changed_array = array(1=>”big dog”, 3=>”bat”, “bird”=>”flamingo”); echo “Changing the value: “; print_r(StaticSample::s_foo($changed_array)); echo “ “;
//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 “ “; echo “Enjoy!”;
?> 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
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().’ ‘; print $b->f().’ ‘; print $c->f().’ ‘; ?>
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: 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 05: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 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:
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,
This can be surprising (it surprised me!) when coming from other object-oriented languages, which would output “doing something with subclass” in this case.