php延迟静态绑定的深入讲解,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 thatstatic:: 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

定义:

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

self与static的区别:

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

示例一(在静态环境下)

?phpclass 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”

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/189980.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-29 13:52
下一篇 2024-11-29 13:52

相关推荐

  • PHP和Python哪个好找工作?

    PHP和Python都是非常流行的编程语言,它们被广泛应用于不同领域的开发中。但是,在考虑择业方向的时候,很多人都会有一个问题:PHP和Python哪个好找工作?这篇文章将从多个方…

    编程 2025-04-29
  • Centos7配置静态ip

    本文将详细阐述如何在Centos7系统中配置静态ip。 一、查看网络接口 在配置静态ip之前,我们首先需要查看系统中的网络接口,以确定我们需要配置的网卡是哪一个。 ifconfig…

    编程 2025-04-29
  • PHP怎么接币

    想要在自己的网站或应用中接受比特币等加密货币的支付,就需要对该加密货币拥有一定的了解,并使用对应的API进行开发。本文将从多个方面详细阐述如何使用PHP接受加密货币的支付。 一、环…

    编程 2025-04-29
  • 使用PHP foreach遍历有相同属性的值

    本篇文章将介绍如何使用PHP foreach遍历具有相同属性的值,并给出相应的代码示例。 一、基础概念 在讲解如何使用PHP foreach遍历有相同属性的值之前,我们需要先了解几…

    编程 2025-04-28
  • PHP获取301跳转后的地址

    本文将为大家介绍如何使用PHP获取301跳转后的地址。301重定向是什么呢?当我们访问一个网页A,但是它已经被迁移到了另一个地址B,此时若服务器端做了301重定向,那么你的浏览器在…

    编程 2025-04-27
  • Python中通过对象不能调用类方法和静态方法的解析

    当我们在使用Python编写程序时,可能会遇到通过对象调用类方法和静态方法失败的问题,那么这是为什么呢?接下来,我们将从多个方面对这个问题进行详细解析。 一、类方法和静态方法的定义…

    编程 2025-04-27
  • Apache伪静态配置Java

    本文将会从多个角度阐述如何在Apache中正确伪装Java应用程序,实现URL的静态化,提高网站的SEO优化和性能。以下是相关的配置和代码实例。 一、RewriteEngine的配…

    编程 2025-04-27
  • PHP登录页面代码实现

    本文将从多个方面详细阐述如何使用PHP编写一个简单的登录页面。 1. PHP登录页面基本架构 在PHP登录页面中,需要包含HTML表单,用户在表单中输入账号密码等信息,提交表单后服…

    编程 2025-04-27
  • PHP与Python的比较

    本文将会对PHP与Python进行比较和对比分析,包括语法特性、优缺点等方面。帮助读者更好地理解和使用这两种语言。 一、语法特性 PHP语法特性: <?php // 简单的P…

    编程 2025-04-27
  • 深入解析Vue3 defineExpose

    Vue 3在开发过程中引入了新的API `defineExpose`。在以前的版本中,我们经常使用 `$attrs` 和` $listeners` 实现父组件与子组件之间的通信,但…

    编程 2025-04-25

发表回复

登录后才能评论