php - 为什么我得到“未捕获的错误:在 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
我想不通是什么想念我。
解决方案
从您的小部件类中的代码来看,该$fb_services
属性似乎已声明但从未初始化。
在register()
方法中,尝试改变这个:
$fb_services = new FacebookServices();
进入:
$this->fb_services = new FacebookServices();
推荐阅读
- javascript - for 循环不显示正确的内容
- r - 如何从 R 中的多个集合中创建一个多重集合?
- html - css 伪 :active => 停止传播
- ios - 在地图视图上用纬度和经度绘制特定位置
- react-native - 如何在 React Native 中定义路由而不在导航器中显示它们?
- html - 动画延迟工作不正常
- typescript - 如何在nestjs中通过socket.io在客户端之间发送图像?
- c# - 多对多 EF Core 已被跟踪 - C# Discord Bot
- excel - 使未列出的数据看起来像在将其放入表格之前
- python - 使用循环和列表检查值 - Python