首页 > 解决方案 > 调用未定义函数 request_currencies()

问题描述

WordPress 5.2.4

class ved_currencies extends WP_Widget {
    function __construct() {
        parent::__construct(
        ‘ved_currencies’,
        "Ved currencies",
        array( 'description' =>'Show currencies'));
    }

    private function request_currencies(){
        $date_req= time();
        $currencies = ["USD", "CNY", "EUR", "JPY", "BYN", "KZT", "UAH"];
    }

    public function widget( $args, $instance ){
        request_currencies(); // Line 30.
        echo "test";
    }
}

结果:

Uncaught Error
: Call to undefined function request_currencies() in
C:\OSPanel\domains\ved\wp-content\plugins\ved-currencies\ved-currencies.php
on line
30

代码示例中标记了第 30 行。

你能帮我理解为什么会出现这个错误吗?

标签: wordpress

解决方案


this关键字在类内部使用,通常与成员函数一起访问当前对象的类(变量或函数)的非静态成员。

class ved_currencies extends WP_Widget {
    function __construct() {
        parent::__construct(
        ‘ved_currencies’,
        "Ved currencies",
        array( 'description' =>'Show currencies'));
    }

    private function request_currencies(){
        $date_req= time();
        $currencies = ["USD", "CNY", "EUR", "JPY", "BYN", "KZT", "UAH"];
    }

    public function widget( $args, $instance ){
        $this->request_currencies(); // Line 30.
        echo "test";
    }
}

我已经使用 $this->request_currencies(); 调用了该函数。

为了您更好地理解,请访问此链接


推荐阅读