Wordpress add_submenu_page won't use class object?
I am trying to make my Wordpress submenu page a function from within a class that extends the parent class. The parent class expects Object
as a parameter in it constructor
.
Object got in parent's constructor and so far seems fine, but when the class later runs the page_form method $this->model
it is null and I get Call to a member function get_primary_key() on a non-object
.. Why?
I am passing a class reference to wordpress add_submenu_page
, is this something I am doing wrong in this code below? Or am I structuring my code in some way?
I want to have a class or method that displays a form that other classes can use and pass a unique object to that form. I don't need help creating the form or objects, but I need some guidance on how to do this inheritance, preferably object oriented.
My parent class:
<?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.
}
}
My main plugin file:
<?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.
);
?>
My booking class:
<?php
use WordPress\ORM\Model\BookingModel;
class Bookings extends Backend
{
function __construct()
{
parent::__construct(new BookingModel());
}
// ...
}
?>
source to share