什么是呼叫转移和静态调用PHP或其他延迟静态绑定? [英] What is Call Forwarding and Static Calls in PHP or otherwise Late static binding?

查看:172
本文介绍了什么是呼叫转移和静态调用PHP或其他延迟静态绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个网站获得了一个代码示例,但我很难理解输出。我分享代码:

  class A 
{
public static function foo()
{
static :: who();


public static function who()
{
echo __CLASS __。\\\
;



class B extends A
{
public static function test()
{
A :: foo ();
parent :: foo();
self :: foo();


public static function who()
{
echo __CLASS __。\\\
;



class C扩展B
{
public static function who()
{
echo __CLASS__。 \\\
;
}
}

C :: test();

输出如下::

  A 
C
C

如果解释了上述输出结果,我会得到很大的帮助。

解决方案


我从网站上获得的一个代码示例,但它很难对于我
来理解输出。我在分享代码


此代码是延迟静态绑定的PHP手册中的精确副本。概念..



手动解释此代码。




静态绑定'的解决方案将停止在完全解析的静态
调用中,而不会回退。另一方面,使用关键字
(如parent ::或self ::)的静态调用将转发调用信息。




所以让我来深入解释一下......



当你这样做时。 C :: test() ; , class B 下的 test()将被调用,因为没有 test()可用于 class C



你很明显在这里..

pre $ public static function test()
{
A :: foo( );
parent :: foo();
self :: foo();
}



案例1: A :: foo();



当你从上面的语句中读到这个时。后期静态绑定的解析将停止在完全解析的静态调用中,因为它是一个完全解析的静态调用,所以您将得到 A



案例2& 3: parent :: foo(); self :: foo();



同样,从上面的声明中可以看出。使用像parent ::或self ::这样的关键字的静态调用将转发调用信息。



所以这显然会打印C和C ..因为既然你做了 C :: test(); class C 是实际的调用者。


One code sample I have got from a website, but it was difficult for me to understand the output. I am sharing the code :

class 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();

The Output is as follows ::

A
C
C

I would be highly helped if the above output is explained. Thanks in Advance.

解决方案

One code sample I have got from a website, but it was difficult for me to understand the output. I am sharing the code

This code is an exact replica from the PHP Manual of the Late Static Binding concept..

Explanation of this code from the manual..

Late static bindings' resolution will stop at a fully resolved static call with no fallback. On the other hand, static calls using keywords like parent:: or self:: will forward the calling information.

Source

So let me explain in depth...

When you do .. C::test(); , The test() under the class B will be called as there is no test() available on class C.

So you are obviously here..

   public static function test() 
   {
      A::foo();
      parent::foo();
      self::foo();
   }

Case 1 : A::foo();

As you read this from the above statement.. Late static bindings' resolution will stop at a fully resolved static call with no fallback , so since it is a fully resolved static call , you will get an output of A

Case 2 & 3 : parent::foo(); and self::foo();

Again, from the above statement.. static calls using keywords like parent:: or self:: will forward the calling information.

So this will obviously print C and C .. because since you did C::test(); , The class C is the actual caller.

这篇关于什么是呼叫转移和静态调用PHP或其他延迟静态绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆