Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 关于后期静态绑定

关于后期静态绑定

Jun 23, 2016 pm 02:02 PM

看了php手册中关于后期静态绑定,还是不太明白,原文地址 http://cn2.php.net/manual/zh/language.oop5.late-static-bindings.php

其中第四个例子

<?phpclass A {    public static function foo() {        static::who();    }    public static function who() {        echo __CLASS__."\n";    }}class B extends A {    public static function test() {        A::foo();        parent::foo();        self::foo();    }    public static function who() {        echo __CLASS__."\n";    }}class C extends B {    public static function who() {        echo __CLASS__."\n";    }}C::test();?>
Copy after login

结果是:
A
C
C
当调用到parent::foo()和self::foo()时,为什么会调用C类的who()?
哪位朋友能解释一下,谢谢。


回复讨论(解决方案)

关于static操作符的解释:
后期静态绑定试图通过引入一个关键字表示运行时最初调用的类来绕过限制。
而这个最初调用的类就是C

A::foo(); 调用的是A类的方法,这你明白
self::foo(); // 这个self实际上是C类。明白吗? C::test() C继承了B的test()方法
parent::foo(); // 由于static::who(); 而不是self::who()。该方法调用的当前的类,也就是C类的foo()方法

也许你觉得还是弄不懂 A类中又为什么没有调用C的who()方法,这是由于parent的特殊关系。延迟静态绑定就是专门为了解决子类与父类间继承方法的问题才出现的。

也不必死记这个概念或者情景,当你遇到一个静态函数的继承与覆盖问题的时候,并发现它不是你想象中样子,那么你加上static再试一下成功了,就OK了。

个人感觉面向对象语言的特性比较个性化,而基本的东西无非封装,多态和继承,更多变态的特性一般是没有需求的,需求的时候也得考虑是不是可靠值得信赖。

手册不是说得很清楚么
------------------------------------------------------
”后期绑定“的意思是说,static::不再被解析为定义 当前方法所在的类,而是在实际运行时计算的。也可以称之为”静态绑定“,因为它可以用于(但不限于)静态方法的调用。
-------------------------------------------------------

#1说的有个小问题

self::foo(); // 这个self实际上是C类。明白吗? C::test() C继承了B的test()方法
Copy after login


不准确,self还是B类,但是本身没有覆写foo方法,所以就调用父类A的foo方法。
如果self实际是C类,那你试下self::foo();改成self::who();,应当打印C,但是打印B,这也正是self和static的区别。

谢谢纠正,学习了 手册不是说得很清楚么
------------------------------------------------------
”后期绑定“的意思是说,static::不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为”静态绑定“,因为它可以用于(但不限于)静态方法的调用。
----------------------------------------------……

A::foo();     //A指代A类,访问A类的foo方法和who方法
parent::foo();//调用B类的父类??A的foo方法,并告诉foo方法最原始的调用者是C
self::foo();  //self指代定义该方法的类,即B,但是B没有定义foo方法,它将原始的调用者C向上传递,
              // 访问父类的foo方法,最后访问c的who方法;


所以这就回答了三楼的疑问:若是把self::foo(); 改成self::who,因为self指代B,而B有who方法,所以结果是变成了B

静态调用使用 parent:: 或者 self:: 将转发原始调用信息。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles