php延迟静态绑定示例分享(php延迟执行)

发布时间:2022-11-14

本文目录一览:

  1. php的后期静态绑定,怎么解释?这幅图输出是A,C,C,。谢谢!我想了很久都不太明白。
  2. PHP后期静态绑定错误问题,怎么解决
  3. [PHP Static延迟静态绑定用法分析](#PHP Static延迟静态绑定用法分析)
  4. [php Late static binding 是什么?](#php Late static binding 是什么?)
  5. PHP继承调用问题,static关键字问题
  6. 如何理解php中的后期静态绑定

php的后期静态绑定,怎么解释?这幅图输出是A,C,C,。谢谢!我想了很久都不太明白。

首先说一下基础的static是相对于public的,面象对像编程时影响继承权限的,这你应该知道。 由图的继承关系可知:C彻底包含了B和A。 在看答案结果以前,他细观察发现,三个类里都有同一个名称who()方法。 系统会用最后一个优先级最高,进一步的说,你几乎没法通过C去调用A、B内的who(),只能重改方法,比如添加个getBWho(){echo B::who();} 然后通过C::getBWho();来调用B内的who(); 下面来看运行结果: test只在B中出现,所以结果必然是test()中运行的三个结果: 第一个:静态直接指名到姓的调用A内静态函数,这没有悬念,必然是A 第二个:parent::是调用上一级的父类,在此题中为A,A中又直接调用static::who();上面说过了,这个who()优先级最高的在C里面,无论在你ABC中哪里调用,只要是static::who()必然是最后定义的那个,覆盖效应,如果想调用A里的必需指明A::who()或是通过去除static从作用域限制来实现。所以这个who()就是C中定义的who 第三个:self::who与第二个类似的问题,看样该走B的,注意覆盖效应,要想调用B内的who必须得B::who(),因为更高级的C已经重写了这个方法,如果C中没有who,肯定就是B,依次类推。所以必然还是调用C中的who; 所以答案为:ACC

PHP后期静态绑定错误问题,怎么解决

可以用于访问静态成员,类常量,还可以用于覆盖类中的属性和方法。 self,parent 和 static 这三个特殊的关键字是用于在类定义的内部对其属性或方法进行访问的。 parent用于调用父类中被覆盖的属性或方法(出现在哪里,就将解析为相应类的父类)。 self用于调用本类中的方法或属性(出现在哪里,就将解析为相应的类;注意与$this区别,$this指向当前实例化的对象)。 当一个子类覆盖其父类中的方法时,PHP 不会调用父类中已被覆盖的方法。是否调用父类的方法取决于子类。

PHP Static延迟静态绑定用法分析

本文实例讲述了PHP Static延迟静态绑定用法。分享给大家供大家参考,具体如下: PHP5.3以后引入了延迟静态绑定static,它是为了解决什么问题呢?php的继承模型中有一个存在已久的问题,那就是在父类中引用扩展类的最终状态比较困难。来看一个例子。

class A {
    public static function echoClass() {
        echo __CLASS__;
    }
    public static function test() {
        self::echoClass();
    }
}
class B extends A {
    public static function echoClass() {
        echo __CLASS__;
    }
}
B::test(); // 输出A

在PHP5.3中加入了一个新特性:延迟静态绑定,就是把本来在定义阶段固定下来的表达式或变量,改在执行阶段才决定,比如当一个子类继承了父类的静态表达式的时候,它的值并不能被改变,有时不希望看到这种情况。 下面的例子解决了上面提出的问题:

class A {
    public static function echoClass() {
        echo __CLASS__;
    }
    public static function test() {
        static::echoClass();
    }
}
class B extends A {
    public static function echoClass() {
        echo __CLASS__;
    }
}
B::test(); // 输出B

第8行的static::echoClass()定义了一个静态延迟绑定方法,直到B调用test的时候才执行原本定义的时候执行的方法。 更多关于PHP相关内容感兴趣的读者可查看本站专题:《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》 希望本文所述对大家PHP程序设计有所帮助。

php Late static binding 是什么?

PHP延迟静态绑定 Late Static Binding 推荐阅读以下内容: As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance. More precisely, late static bindings work by storing the class named in the last "non-forwarding call". In case of static method calls, this is the class explicitly named (usually the one on the left of the :: operator); in case of non static method calls, it is the class of the object. A "forwarding call" is a static one that is introduced by self::, parent::, static::, or, if going up in the class hierarchy, forward_static_call(). The function get_called_class() can be used to retrieve a string with the name of the called class and static:: introduces its scope. This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information. It was also called a "static binding" as it can be used for (but is not limited to) static method calls.

PHP继承调用问题,static关键字问题

问题出现在,A类中调用静态方法的写法static::who(); 通常调用静态方法使用 ClassName::foo() 或者 self::foo(); 自PHP 5.3.0 起,PHP 增加了一个叫做后期静态绑定的功能,用于在继承范围内引用静态调用的类。 “后期绑定”的意思是说,static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。 详情 自己 搜索一下PHP手册

如何理解php中的后期静态绑定

使用的保留关键字:

  • static 定义: static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。

self与static的区别:

  • self 调用的就是本身代码片段这个类。
  • static 调用的是从堆内存中提取出来,访问的是当前实例化的的那个类(即static作用于当前调用的类)。

示例一(在静态环境下)

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(); // 输出结果是 "B"