Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在函数usort中引用函数_Php_Sorting_Usort - Fatal编程技术网

Php 如何在函数usort中引用函数

Php 如何在函数usort中引用函数,php,sorting,usort,Php,Sorting,Usort,我试图使用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_lates

我试图使用
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
以引用我的“奖励”功能时

它抛出了一个错误,即公共奖励函数中的my函数已经声明


如何正确指向
\u mt\u latest\u award\u sort
函数?

您的函数位于其他函数中,请尝试在类中声明您的函数

例如:

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()
    {
        ...
    }
}

PHP不支持嵌套函数

这是一个常见的错误/误解,因为下面的代码似乎有效:

function foo() {
    function bar() {
        echo 'Hello!';
    }
    bar();
    bar();
    bar();
}
然而,这实际上并不是定义一个嵌套函数,它只是在运行
foo()
时定义一个全局函数
bar
。您可以通过运行两次
foo();第二次,您将得到一个致命错误,因为您试图定义同一个函数两次

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

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 
        ...
    }
}
或者,使用:

调用方法“awards”时,将尝试声明函数“\u mt\u latest\u awards\u sort”

使用
$awards=usort($q->posts,array($this,“\u mt\u latest\u awards\u 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) {
    // ...
});

您发布的代码中没有
\u mt\u latest\u award\u sort
函数。您的意思是
\u mt\u latest\u award
?如果您希望将其改为类函数(就像
awards()
is),或者如果(!function\u exists(“\u mt\u latest\u award\u 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) {
    // ...
});