看了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();?>
结果是:
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()方法
不准确,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:: 将转发原始调用信息。

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
