PHP作用域限定符::的几个程序例子
2013-11-02 13:29
375 查看
双冒号::被认为是作用域限定操作符,用来指定类中不同的作用域级别。::左边表示的是作用域,右边表示的是访问的成员。
系统定义了两个作用域,self和parent。self表示当前类的作用域,在类之外的代码是不能使用这个操作符的。
程序运行结果:
parent这个作用域很简单,就是派生类用来调用基类的成员时候使用。
程序运行结果:
程序运行结果:
程序运行结果:
程序运行结果:
系统定义了两个作用域,self和parent。self表示当前类的作用域,在类之外的代码是不能使用这个操作符的。
Program List:使用self作用域访问父类中的函数
01 | <?php |
02 | class NowaClass |
03 | { |
04 | function nowaMethod() |
05 | { |
06 | '我在类 NowaClass 中声明了。' ; |
07 | } |
08 | } |
09 |
10 | class ExtendNowaClass extends NowaClass |
11 | { |
12 | function extendNowaMethod() |
13 | { |
14 | 'extendNowaMethod 这个方法在 ExtendNowaClass这个类中声明了。' ; |
15 | self::nowaMethod(); |
16 | } |
17 | } |
18 |
19 | ExtendNowaClass::extendNowaMethod(); |
20 |
21 | ?> |
1 | extendNowaMethod 这个方法在 ExtendNowaClass这个类中声明了。 |
2 | 我在类 NowaClass 中声明了。 |
Program List:使用parent作用域
01 | <?php |
02 | class NowaClass |
03 | { |
04 | function nowaMethod() |
05 | { |
06 | '我是基类的函数。' ; |
07 | } |
08 | } |
09 |
10 | class ExtendNowaClass extends NowaClass |
11 | { |
12 | function extendNowaMethod() |
13 | { |
14 | '我是派生类的函数。' ; |
15 | parent::nowaMethod(); |
16 | } |
17 | } |
18 |
19 | $obj = new ExtendNowaClass(); |
20 | $obj -> extendNowaMethod(); |
21 |
22 | ?> |
1 | 我是派生类的函数。 |
2 | 我是基类的函数。 |
Program List:用基类的方法访问派生类的静态成员
如何继承一个存储位置上的静态属性。01 | <?php |
02 | class Fruit |
03 | { |
04 | public function get() |
05 | { |
06 | echo $this ->connect(); |
07 | } |
08 | } |
09 | class Banana extends Fruit |
10 | { |
11 | private static $bananaColor ; |
12 | public function connect() |
13 | { |
14 | return self:: $bananaColor
'yellow' ; |
15 | } |
16 | } |
17 | class Orange extends Fruit{ |
18 | private static $orange_color ; |
19 | public function connect() |
20 | { |
21 | return self:: $orange_color
'orange' ; |
22 | } |
23 | } |
24 | $banana = new Banana(); |
25 | $orange = new Orange(); |
26 | $banana ->get(); |
27 | $orange ->get(); |
28 | ?> |
1 | yellow |
2 | orange。 |
Program List:静态函数初始化
在一个类中初始化静态变量比较复杂,你可以通过创建一个静态函数创建一个静态的构造器,然后在类声明后马上调用它来实现初始化。01 | <?php |
02 | class Fruit |
03 | { |
04 | private static $color
"White" ; |
05 | private static $weigth ; |
06 | public static function init() |
07 | { |
08 | echo self:: $weigth
$color . " Kilogram!" ; |
09 | } |
10 | } |
11 | Fruit::init(); |
12 | ?> |
1 | White Kilogram! |
Program List:一个简单的单例模式例子
这个应该可以帮到某些人吧。01 | <?php |
02 | class Fruit |
03 | { |
04 | private static $instance
|
05 | private function __construct() |
06 | { |
07 | $this -> color = 'Green' ; |
08 | } |
09 | public static function getInstance() |
10 | c799 { |
11 | if (self:: $instance ==null) |
12 | { |
13 | "Fruitobject created!<br />" ; |
14 | self:: $instance = new self; |
15 | } |
16 | return self:: $instance ; |
17 | } |
18 | public function showColor() |
19 | { |
20 | "My color is {$this-> color}!<br>" ; |
21 | } |
22 | public function setColor( $color ) |
23 | { |
24 | $this -> color = $color ; |
25 | } |
26 | } |
27 | $apple =Fruit::getInstance(); // Fruitobject created! |
28 | $apple -> showColor(); // My color is Green! |
29 | $apple -> setColor( "Red" ); |
30 | $apple -> showColor(); // My color is Red! |
31 | $banana =Fruit::getInstance(); |
32 | $banana -> showColor(); // My color is Red! |
33 | $banana -> setColor( "Yellow" ); |
34 |
35 | $apple -> showColor(); // My color is Yellow! |
36 | ?> |
1 | Fruitobject created! |
2 | My color is Green! |
3 | My color is Red! |
4 | My color is Red! |
5 | My color is Yellow! |
相关文章推荐
- PHP作用域限定符::的几个程序例子
- 几个PHP的静态方法的程序例子
- php调用c++程序例子
- php计算程序运行时间的简单例子分享
- 从几个简单的程序看PHP的垃圾回收机制
- 总结了下PHPExcel官方读取的几个例子
- FLEX FOR PHP的博客以及例子程序
- linux中查找php***程序例子
- linux中查找php***程序例子
- 几个简单的程序看PHP的垃圾回收机制
- php中几个简单的例子
- RPC框架Thrift例子-PHP调用C++后端程序
- php冒泡排序实现方法,传入几个数字排序后 输出实战例子
- 收藏几个JQuery解析XML的程序例子
- 在浏览器中 编译php程序例子时,老是报NOTICE错误
- 工作中的几个程序例子
- PHP网站安全,PHP程序安全的几个措施
- php调用c++程序例子
- PHP网站安全,PHP程序安全的几个措施
- 几个PHP 小程序