首页 > 解决方案 > 为什么我得到“未捕获的错误:在 null 上调用成员函数 getFbPage()”?

问题描述

我正在尝试在 Wordpress 中注册我的自定义小部件。不幸的是,我在与另一个班级的交流中遇到了问题FacebookServices。我不知道为什么我仍然收到错误Uncaught Error: Call to a member function getFbPage() on null

这是一个类,我在其中创建非常标准的小部件,直到我想将 html 代码调用到我的函数中widget()

<?php
/**
 * Facebook Page widget
 */

namespace Inc\Api\Widgets;

use WP_Widget;
use Inc\Plugins\FacebookServices;



class FbPage extends WP_Widget
{
    public $fb_services;

    public $widget_ID;
    public $widget_name;
    public $widget_options = array();
    public $control_options = array();



    public function __construct()
    {
        $this->widget_ID = 'fb_page';
        $this->widget_name = 'Facebook Page';
        $this->widget_options = array(
            'classname'                     => $this->widget_ID,
            'description'                   => 'Widget of Facebook Page',
            'customize_selective_refresh'   => true
        );
        $this->control_options = array();
    }



    public function register()
    {
        parent::__construct( $this->widget_ID, $this->widget_name, $this->widget_options, $this->control_options );

        add_action( 'widgets_init', array( $this, 'widgetInit') );

        $fb_services = new FacebookServices();
    }



    public function widgetInit()
    {
        register_widget( $this );
    }



    public function widget( $args, $instance )
    {
        echo $this->fb_services->getFbPage();   // <= line 58 from error
    }
...

这是调用类:

<?php
/**
 * Ensure Facebook integration
 */

namespace Inc\Plugins;

class FacebookServices
{
    public $url;

    public function __construct()
    {
        $this->url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    }



    public function theFbLikes()
    {   
        echo '<div class="fb-plugin-wrapper js-fbPlugin likes rendering">';
        echo '<div class="fb-like" data-href="' . $this->url . '" data-layout="standard" data-action="like" data-size="small" data-show-faces="true" data-share="true"></div>';
        echo '</div>';
    }



    public function getFbPage()
    {
        return 'test';
    }
}

我想得到'test'但每次我调用函数时getFbPage()都会出错:

Fatal error: Uncaught Error: Call to a member function getFbPage() on null in C:\xampp\htdocs\dev1\wp-content\themes\tomva\inc\Api\Widgets\FbPage.php:58 Stack trace: #0 C:\xampp\htdocs\dev1\wp-includes\class-wp-widget.php(372): Inc\Api\Widgets\FbPage->widget(Array, Array) #1 C:\xampp\htdocs\dev1\wp-includes\widgets.php(743): WP_Widget->display_callback(Array, Array) #2 C:\xampp\htdocs\dev1\wp-content\themes\tomva\single.php(56): dynamic_sidebar('static') #3 C:\xampp\htdocs\dev1\wp-includes\template-loader.php(74): include('C:\\xampp\\htdocs...') #4 C:\xampp\htdocs\dev1\wp-blog-header.php(19): require_once('C:\\xampp\\htdocs...') #5 C:\xampp\htdocs\dev1\index.php(17): require('C:\\xampp\\htdocs...') #6 {main} thrown in C:\xampp\htdocs\dev1\wp-content\themes\tomva\inc\Api\Widgets\FbPage.php on line 58

我想不通是什么想念我。

标签: phpwordpress

解决方案


从您的小部件类中的代码来看,该$fb_services属性似乎已声明但从未初始化。

register()方法中,尝试改变这个:

$fb_services = new FacebookServices();

进入:

$this->fb_services = new FacebookServices();


推荐阅读