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

双冒号::被认为是作用域限定操作符,用来指定类中不同的作用域级别。::左边表示的是作用域,右边表示的是访问的成员。

系统定义了两个作用域,self和parent。self表示当前类的作用域,在类之外的代码是不能使用这个操作符的。

Program List:使用self作用域访问父类中的函数
01<?php
02    class NowaClass
03    {
04        function nowaMethod()
05        {
06            print '我在类 NowaClass 中声明了。';
07        }
08    }
09     
10    class ExtendNowaClass extends NowaClass
11    {
12        function extendNowaMethod()
13        {
14            print 'extendNowaMethod 这个方法在 ExtendNowaClass 这个类中声明了。';
15            self::nowaMethod();
16        }
17    }
18     
19    ExtendNowaClass::extendNowaMethod();
20     
21?>

程序运行结果:

1extendNowaMethod 这个方法在 ExtendNowaClass 这个类中声明了。
2我在类 NowaClass 中声明了。

parent这个作用域很简单,就是派生类用来调用基类的成员时候使用。

Program List:使用parent作用域
01<?php
02    class NowaClass
03    {
04        function nowaMethod()
05        {
06            print '我是基类的函数。';
07        }
08    }
09     
10    class ExtendNowaClass extends NowaClass
11    {
12        function extendNowaMethod()
13        {
14            print '我是派生类的函数。';
15            parent::nowaMethod();
16        }
17    }
18     
19    $obj = new ExtendNowaClass();
20    $obj -> extendNowaMethod();
21     
22?>

程序运行结果:

1我是派生类的函数。
2我是基类的函数。
Program List:用基类的方法访问派生类的静态成员

如何继承一个存储位置上的静态属性。

01<?php
02class Fruit
03{
04    public function get()
05    {
06        echo $this->connect();
07    }
08}
09class Banana extends Fruit
10{
11    private static $bananaColor;
12    public function connect()
13    {
14        return self::$bananaColor = 'yellow';
15    }
16}
17class 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?>

程序运行结果:

1yellow
2orange。
Program List:静态函数初始化

在一个类中初始化静态变量比较复杂,你可以通过创建一个静态函数创建一个静态的构造器,然后在类声明后马上调用它来实现初始化。

01<?php
02class Fruit
03{
04    private static $color = "White";
05    private static $weigth;
06    public static function init()
07    {
08        echo self::$weigth = self::$color . " Kilogram!";
09    }
10}
11Fruit::init();
12?>

程序运行结果:

1White Kilogram!
Program List:一个简单的单例模式例子

这个应该可以帮到某些人吧。

01<?php
02class Fruit
03{
04    private static $instance = null;
05    private function __construct()
06    {
07      $this-> color = 'Green';
08    }
09    public static function getInstance()
10    {
11        if(self::$instance == null)
12        {
13            print "Fruit object created!<br />";
14            self::$instance = new self;
15        }
16        return self::$instance;
17    }
18    public function showColor()
19    {
20        print "My color is {$this-> color}!<br>";
21    }
22    public function setColor($color)
23    {
24        $this-> color = $color;
25    }
26}
27  $apple = Fruit::getInstance(); // Fruit object 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?>

程序运行结果:

1Fruit object created!
2My color is Green!
3My color is Red!
4My color is Red!
5My color is Yellow!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值