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