douzi1986 2015-06-07 09:47
浏览 18

Wordpress add_submenu_page不会使用类对象?

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());
    } 

    // ...

}

?>
  • 写回答

1条回答 默认 最新

  • douju6542 2015-06-07 19:41
    关注

    you forget the $ of $model in the constructor of Backend ?

    $this->model = $model;
    

    when adding this $, I can access $this->model in "page_form"

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)