PHP Class self 与 static 异同与使用详解

吾爱主题 阅读:181 2021-11-24 16:00:00 评论:0

对于大多数 PHPer 来说,self 与 static 两个 PHP 关键词都不算陌生。我们学会通过self::xxxx这种方式来调用当前类的静态属性和方法。而 static 呢?想必很多人只知道它是用于定义一个静态方法和类属性关键词。

这也是我之前的认知。

现在我们来回顾一下这两个关键词的一些常见用法:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // self 用法 1 :调用静态成员属性     class Person   {        protected static $maxAddressCount = 5; // 收获地址创建最大数量。            public function test()        {            echo self:: $maxAddressCount ;        }   }       $person = new Person();   $person ->test();
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 // self 用法 2 :调用静态方法     class Person   {        protected static $maxAddressCount = 5; // 收获地址创建最大数量。            protected static function getMaxAddressCount()        {            return self:: $maxAddressCount ;        }            public function test()        {            echo self::getMaxAddressCount();        }   }       $person = new Person();   $person ->test();
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 // self 用法 3 :创建一个当前对象     // 单例示例   class Person   {        private static $instance = null;            private function __construct() {}            final public static function getInstance()        {            if (self:: $instance == null) {                self:: $instance = new self;            }            return self:: $instance ;        }            public function test()        {            echo "hello world!" ;        }   }       $person = Person::getInstance();   $person ->test();

关于 static 关键词的常见用法也在上面 3 个示例中得到综合体现

我深信上面的用法,任何一个入门的 PHPer 都是非常熟悉的。现在我要讲的是以下两种方式:

new self()new static() 的区别?

我相信很多人都知道new self()创建一个当前类的对象,并不知道new static()也能创建一个当前类的对象。

关于new static()这种用法呢,在官方文档有说明。地址:https://www.php.net/manual/zh/language.oop5.late-static-bindings.php

PHP 官方把这种方式称为:后期静态绑定。

官方示例 1:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 class A {        public static function who() {            echo __CLASS__ ;        }        public static function test() {            self::who();        }   }       class B extends A {        public static function who() {            echo __CLASS__ ;        }   }       B::test();

因为 Class B 继承了 Class A。 A 与 B 都有一个静态方法who()。此时通过B::test()的时候,调用的实际上是 Class A 的who()方法。

因为子类 Class B 的静态方法who()属于在 Class A 之后的子类里面才定义的。而 PHP 的默认特性只允许调用最先定义的。

就这引出了后期静态绑定的概念。

官方示例 2:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 class A {        public static function who() {            echo __CLASS__ ;        }        public static function test() {            static ::who(); // 后期静态绑定从这里开始        }   }       class B extends A {        public static function who() {            echo __CLASS__ ;        }   }       B::test();

我们把 Class A 里面的test()方法体的self更改为static之后,static 代表的永远是指向调用类。也就是说虽然在 Class A 父类里面定义的方法与子类有同名冲突的情况。但是,当子类调用的时候,那么自动切换到子类的静态同名方法。取决于调用者。

大家可以通过运行以上两个示例进行理解。

之所以会有本篇小节内容。是因为我在实际运行当中要继承单例方法导致了这个问题。所以,才牵扯出这个特性。

到此这篇关于PHP Class self 与 static 异同与使用详解的文章就介绍到这了,更多相关PHP Class self 与 static 内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.php.cn/php-weizijiaocheng-481717.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容