久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP 錯誤地處理了我的靜態調用

PHP is handling incorrectly my static call(PHP 錯誤地處理了我的靜態調用)
本文介紹了PHP 錯誤地處理了我的靜態調用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在 PHP 5.3 上遇到了問題.我需要使用 __callStatic 調用一個方法,但是如果我在實例化對象中使用它,PHP 會調用 __call 代替.

I'm havinh a problem on PHP 5.3. I need to call a method by using __callStatic, but if I use it in a instancied object, PHP call __call instead.

以上現實生活中的例子:

Above a real life example:

<?php

    class A {
        function __call($method, $args){
            // Note: $this is defined!
            echo "Ops! Don't works. {$this->c}";
        }

        static function __callStatic($method, $args){
            echo 'Fine!';
        }
    }

    class B extends A {
        public $c = 'Real Ops!';

        function useCallStatic(){
            static::foo();
            // === A::foo();
            // === B::foo();
        }
    }

    $foo = new B();
    $foo->useCallStatic();

    // This works:
    // B::foo();

?>

打印:操作!不工作.真正的行動!

Prints: Ops! Don't works. Real Ops!

請注意,我從 new B() 調用了 useCallStatic.如果我直接調用像 B::foo().

Note that I call useCallStatic from a new B(). If I call directly works fine like B::foo().

我能做些什么才能正常工作?

What I can do to it works fine?

推薦答案

仔細想了想,其實不是bug.但這絕對不直觀.

After thinking about it, it's not really a bug. But it's definitely unintuitive.

<?php
class A
{
  public function foo()
  {
    static::bar();
  }

  public function bar()
  {
    echo "bar()
";
  }
}

$a = new A();
$a->foo();

這是有效的,并調用 bar().這并不意味著靜態調用欄.表示查找當前的類名,如果存在則調用函數bar,無論是否為靜態函數.

This is valid, and calls bar(). It does not mean call bar statically. It means to look up the current class name and call function bar if it exists, whether or not it's a static function.

再澄清一點:您認為 parent::bar() 是否意味著調用名為 bar() 的靜態函數?不,這意味著調用任何名為 bar() 的函數.

To clarify a bit more: do you think parent::bar() means to call a static function called bar()? No, it means call whatever function is named bar().

考慮:

<?php
class A
{
  function __call($name, $args)
  {
    echo "__call()
";
  }

  static function __callStatic($name, $ags)
  {
    echo "__callStatic()
";
  }

  function regularMethod()
  {
    echo "regularMethod()
";
  }

  static function staticMethod()
  {
    echo "staticMethod()
";
  }

}

class B extends A
{
  function foo()
  {
    parent::nonExistant();   
    static::nonExistant();
    parent::regularMethod();
    parent::staticMethod(); 
  }
}

$b = new B();
$b->foo();

parent::nonExistant() 方法調用 A::__call()static::nonExistant() 也是如此.為 任一 調用調用 A::__callStatic() 將同樣有效!PHP 無法知道您要調用哪一個.但是,按照設計,PHP 在從此類上下文調用時給予 __call 優先權.

The parent::nonExistant() method invokes A::__call(), as does static::nonExistant(). Invoking A::__callStatic() for either call would be equally as valid! There is no way for PHP to know which one you want to be called. However, by design, PHP gives __call the priority when invoked from this sort of context.

parent::regularMethod() 調用非靜態函數 regularMethod().(同樣,:: 操作符并不意味著調用這個靜態函數".)同樣,parent::staticMethod() 調用 A::staticMethod() 正如人們所期望的那樣.

parent::regularMethod() invokes the non static function regularMethod(). (Again, the :: operator does not mean "call this static function.") Likewise parent::staticMethod() invokes A::staticMethod() as one might expect.

解決您的問題:

您可以在繼承的類中手動調用self::__callStatic('foo').

You could manually call self::__callStatic('foo') in the inherited class.

或者在 A 類的 __call 方法中過濾已知列表并調用 self::__callStatic.

Or within the __call method of class A filter against a known list and call self::__callStatic.

function __call($f, $a)
{
  if ($f == 'foo')
    return self::__callStatic($f, $a); 
  else
  {
  }
}

這很丑陋,但至少擴展類的人不需要做任何特別的事情.

It's ugly, but at least people who extend the class won't need to do anything special.

這篇關于PHP 錯誤地處理了我的靜態調用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 欧美白人做受xxxx视频 | 久久久久久久久久久久久久国产 | 91国在线高清视频 | 美女爽到呻吟久久久久 | 久久综合伊人 | 精品视频久久久久久 | 精品国产欧美一区二区三区不卡 | www.99热这里只有精品 | 国产精品永久免费 | 中国美女撒尿txxxxx视频 | 欧美一区二区免费 | 久久综合久色欧美综合狠狠 | 国产成人一区二区三区久久久 | 久久69精品久久久久久久电影好 | 伊人春色成人 | 一级爱爱片 | 国产精品免费一区二区三区四区 | 九九九视频在线 | 一本一道久久a久久精品综合 | 精品日韩一区 | 日韩国产欧美一区 | 美女久久久久久久 | 性欧美精品一区二区三区在线播放 | 99热这里都是精品 | 日本亚洲一区 | 激情亚洲 | 色免费视频 | 日韩性生活网 | 在线播放国产一区二区三区 | 日本一道本视频 | 久热m3u8 | 国产二区在线播放 | 精品久草| 亚洲高清在线 | 中文字幕国产第一页 | 欧美成人精品一区二区男人看 | 久久精彩视频 | 国产精品.xx视频.xxtv | 欧美综合一区二区三区 | 精品成人一区二区 | 亚洲日韩中文字幕一区 |