Php Wordpress添加子菜单页面赢得';不使用类对象吗?
我试图让我的Wordpress子菜单页面从类内调用一个函数,它扩展了父类。父类需要一个Php Wordpress添加子菜单页面赢得';不使用类对象吗?,php,wordpress,Php,Wordpress,我试图让我的Wordpress子菜单页面从类内调用一个函数,它扩展了父类。父类需要一个对象作为其构造函数中的参数 该对象是在父对象的构造函数中接收的,到目前为止似乎还可以,但是当类稍后运行方法页时,该方法页_form$this->model为null,并且我对非对象调用了成员函数get\u primary\u key()。。为什么? 我正在将一个类引用传递到wordpress添加子菜单页面,我在下面的代码中是否做错了什么?或者我的代码结构在某种程度上是错误的 我希望有一个类或方法来呈现一个表单,
对象
作为其构造函数
中的参数
该对象是在父对象的构造函数中接收的,到目前为止似乎还可以,但是当类稍后运行方法页时,该方法页_form$this->model
为null,并且我对非对象调用了成员函数get\u primary\u key()。。为什么?
我正在将一个类引用传递到wordpress添加子菜单页面
,我在下面的代码中是否做错了什么?或者我的代码结构在某种程度上是错误的
我希望有一个类或方法来呈现一个表单,其他类可以使用它并将唯一的对象传递给这个表单。我不需要帮助自己创建表单或对象,但我需要一些关于如何进行这种继承的指导,最好是面向对象的
我的家长班级:
<?php
class Backend
{
protected $model;
function __construct($model)
{
$this->model = model;
echo $this->model->get_primary_key() // this works fine. returns 'id' as string.
}
function page_form()
{
echo $this->model->get_primary_key(); // this gives me error, Call to a member function get_primary_key() on a non-object.
// This function should render a form,
// using parameters inside $this->model, but its NULL.
}
}
<?php
// ...
if(is_admin()) {
add_action( 'admin_menu', 'my_plugin_menu' );
}
function my_plugin_menu() {
add_menu_page(
'My plugin', // page-title
'My plugin', // label
'manage_options',
'my-plugin-menu', // unique-handle
'settings_start' // function
);
$bookings = new Bookings();
add_submenu_page(
'my-plugin-menu', // parent unique-handle
'Add new Booking', // page-title
'Add new Booking', // label
'manage_options',
'my-plugin-menu-add-booking', // submenu unique-handle
array(&$bookings, 'page_form') // this is the function to call, defined in parent class.
);
?>
<?php
use WordPress\ORM\Model\BookingModel;
class Bookings extends Backend
{
function __construct()
{
parent::__construct(new BookingModel());
}
// ...
}
?>
我的预订类别:
<?php
class Backend
{
protected $model;
function __construct($model)
{
$this->model = model;
echo $this->model->get_primary_key() // this works fine. returns 'id' as string.
}
function page_form()
{
echo $this->model->get_primary_key(); // this gives me error, Call to a member function get_primary_key() on a non-object.
// This function should render a form,
// using parameters inside $this->model, but its NULL.
}
}
<?php
// ...
if(is_admin()) {
add_action( 'admin_menu', 'my_plugin_menu' );
}
function my_plugin_menu() {
add_menu_page(
'My plugin', // page-title
'My plugin', // label
'manage_options',
'my-plugin-menu', // unique-handle
'settings_start' // function
);
$bookings = new Bookings();
add_submenu_page(
'my-plugin-menu', // parent unique-handle
'Add new Booking', // page-title
'Add new Booking', // label
'manage_options',
'my-plugin-menu-add-booking', // submenu unique-handle
array(&$bookings, 'page_form') // this is the function to call, defined in parent class.
);
?>
<?php
use WordPress\ORM\Model\BookingModel;
class Bookings extends Backend
{
function __construct()
{
parent::__construct(new BookingModel());
}
// ...
}
?>
您忘记了后端构造函数中的$of$model吗
$this->model = $model;
添加此$时,我可以在“页面表单”中访问$this->model