您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP作用域限定符::的几个程序例子_PHP教程

2024/3/20 13:05:06发布27次查看
双冒号::被认为是作用域限定操作符,用来指定类中不同的作用域级别。::左边表示的是作用域,右边表示的是访问的成员。
系统定义了两个作用域,self和parent。self表示当前类的作用域,在类之外的代码是不能使用这个操作符的。
program list:使用self作用域访问父类中的函数
程序运行结果:
extendnowamethod 这个方法在 extendnowaclass 这个类中声明了。我在类 nowaclass 中声明了。
parent这个作用域很简单,就是派生类用来调用基类的成员时候使用。
program list:使用parent作用域 extendnowamethod(); ?>
程序运行结果:
我是派生类的函数。我是基类的函数。
program list:用基类的方法访问派生类的静态成员 如何继承一个存储位置上的静态属性。
connect(); }}class banana extends fruit { private static $bananacolor; public function connect() { return self::$bananacolor = 'yellow'; }}class orange extends fruit { private static $orange_color; public function connect() { return self::$orange_color = 'orange'; }}$banana = new banana();$orange = new orange();$banana->get();$orange->get();?>
程序运行结果:
yelloworange。
program list:静态函数初始化 在一个类中初始化静态变量比较复杂,你可以通过创建一个静态函数创建一个静态的构造器,然后在类声明后马上调用它来实现初始化。

程序运行结果:
white kilogram!
program list:一个简单的单例模式例子 这个应该可以帮到某些人吧。
color = 'green'; } public static function getinstance() { if(self::$instance == null) { print fruit object created!
; self::$instance = new self; } return self::$instance; } public function showcolor() { print my color is {$this-> color}!
; } public function setcolor($color) { $this-> color = $color; }} $apple = fruit::getinstance(); // fruit object created! $apple-> showcolor(); // my color is green! $apple-> setcolor(red); $apple-> showcolor(); // my color is red! $banana = fruit::getinstance(); $banana-> showcolor(); // my color is red! $banana-> setcolor(yellow); $apple-> showcolor(); // my color is yellow!?>
程序运行结果:
fruit object created!my color is green!my color is red!my color is red!my color is yellow!
http://www.bkjia.com/phpjc/752398.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/752398.htmltecharticle双冒号::被认为是作用域限定操作符,用来指定类中不同的作用域级别。::左边表示的是作用域,右边表示的是访问的成员。 系统定义了两...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product