Я пытаюсь зарегистрировать свой пользовательский виджет в 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
Я не могу понять, что скучаю по мне.