【问题标题】:How to reference a function within a function usort如何在函数 usort 中引用函数
【发布时间】:2014-09-24 22:38:04
【问题描述】:

我正在尝试使用 usort 调用函数,但是,我收到一个错误,指出该类没有方法。

$awards = usort ($q->posts, array($this, '_mt_latest_award_sort'));

目前的设置是:

namespace Test\Theme
class Shortcodes extends Theme
{
    // ...
    // this is the function I am trying to call on
    function _mt_latest_award_sort($value1, $value2) 
    {
        // ...
        // code to do the sorting 
        // ...
    }
    // ...
    $awards = usort ($q->posts, array($this, '_mt_latest_award_sort');
    // ...
}

当我更改 usort 以引用我的“奖励”函数时

$awards = usort ($q->posts, array($this, 'awards'));

它会返回一个错误,即我在公共奖励函数中的函数已被声明。

如何正确指向_mt_latest_award_sort 函数?

【问题讨论】:

  • 您发布的代码中没有 _mt_latest_award_sort 函数。你的意思是_mt_latest_award
  • 如果您希望可以将其设为类函数(就像awards() 一样),或者您可以将函数声明包含在if (!function_exists('_mt_latest_award_sort')) { } 语句中。

标签: php sorting usort


【解决方案1】:

你的函数在另一个函数中,试着在类中声明你的函数

例如:

class C
{
    public function Fa()
    {
        function Fc()
        {
            ...
        }
        ...
        $this->Fb(); // it works C::Fb is a function
        $this->Fc(); // it doesn't work, there is no C::Fc function
        ...
    }

    function Fb()
    {
        ...
    }
}

【讨论】:

    【解决方案2】:

    PHP 不支持嵌套函数。

    这是一个常见的错误/误解,因为下面的代码似乎可以工作:

    function foo() {
        function bar() {
            echo 'Hello!';
        }
        bar();
        bar();
        bar();
    }
    

    然而,这实际上并不是定义一个嵌套函数,它只是在运行foo() 时定义一个全局函数bar。你可以通过运行foo() 两次来测试它——第一次,function bar 将被定义,然后运行 ​​3 次;第二次,你会得到一个致命错误,因为你试图定义相同的函数两次。

    您需要将您的函数定义为类的单独方法:

    class Shortcodes extends Theme
    {
        public function awards()
        {
           ...
            $awards = usort($q->posts, array($this, '_mt_latest_award_sort'));
            ...
        }
    
        private function _mt_latest_award_sort($value1, $value2) 
        {
            ...
            //code to do the sorting 
            ...
        }
    }
    

    或者,使用anonymous function

    class Shortcodes extends Theme
    {
        public function awards()
        {
            ...
            $awards = usort ($q->posts, function($value1, $value2) 
            {
                ...
                //code to do the sorting 
                ...
            });
            ...
        }
    }
    

    【讨论】:

    • 谢谢,将函数移出到类的单独方法中。
    【解决方案3】:

    当调用方法“awards”时,会尝试声明函数“_mt_latest_award_sort”。

    使用$awards = usort ($q->posts, array($this, '_mt_latest_award_sort'); 实现,这也会以同样的方式失败:

    $shortcode = new Shorcodes;
    $shortcode->awards(); // "_mt_latest_award_sort" is declared
    $shortcode->awards(); // attempted to redeclare "_mt_latest_award_sort"
    

    仅仅因为你在另一个函数中声明了一个命名函数,并不意味着它的作用域不是全局的。

    这应该可以解决您的问题:

    class Shortcodes extends Theme
    {
        private $posts = array();
    
        // ...
    
        private function _mt_latest_award_sort($value1, $value2) {
             // ...
        }
    
        public function awards()
        {
             // ...
            $awards = usort($this->posts, array($this, '_mt_latest_award_sort'));
             // ...
        }
    }
    

    你也可以使用匿名函数:

    $awards = usort($this->posts, function($value1, $value2) {
        // ...
    });
    

    【讨论】:

      猜你喜欢
      • 2012-02-14
      • 1970-01-01
      • 2018-11-11
      • 1970-01-01
      • 2010-11-08
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      相关资源
      最近更新 更多