创建插件时未定义的方法Class::Construct()

时间:2018-05-30 作者:stckvrw

我尝试创建一个遵循guid的简单插件http://docs.layerswp.com/plugin-creation-guide

所以我创建了主文件functions.php

<?php /*
* Plugin Name: Layers - Demo Extension
* Version: 1.0
* Plugin URI: http://www.yourpluginpage.com
* Description: What does your Extension do for Layers?
* Author: You
* Author URI: http://www.yoursite.com/
*
* Requires at least: 4.5
* Tested up to: 4.5.3
*
* Layers Plugin: True
* Layers Required Version: 1.5.4
*
* Text Domain: layers-demo-plugin
* Domain Path: /lang/
*/
if ( ! defined( \'ABSPATH\' ) ) exit;

// define constants (optional)

// Load plugin class files
require_once( \'includes/class-layers-demo-extension.php\' );

// Instantiate Plugin
function layers_demo_extension_init() {
    global $layers_demo_extension;
    $layers_demo_extension = Layers_Demo_Extension::get_instance();
}
add_action( \'plugins_loaded\', \'layers_demo_extension_init\' );

// Additional Functionality
和文件includes/class-layers-demo-extension.php

<?php /**
* Layers Demo Extension
*
* What your plugin does
*
* @package Layers
* @since Layers 1.2.4
*/

class Layers_Demo_Extension {

//Initiator
private static $instance;

public static function get_instance() {
    if ( ! self::$instance ) {
        self::$instance = new self();
        self::$instance->construct();
    }
    return self::$instance;
}

// Constructor
private function __construct() {
    // Hooks go here
}

//Custom Functions

}
但当我转到管理面板时,我得到了500错误。

当我启用时WP_DEBUG 我看到这个了

Fatal error: Uncaught Error: Call to undefined method Layers_Demo_Extension::construct() in /.../wp-content/plugins/layers-demo-extension/includes/class-layers-demo-extension.php:18 Stack trace:
#0 /.../wp-content/plugins/layers-demo-extension/functions.php(29): Layers_Demo_Extension::get_instance()
#1 /.../wp-includes/class-wp-hook.php(286): layers_demo_extension_init(\'\')
#2 /.../wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#3 /.../wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#4 /.../wp-settings.php(327): do_action(\'plugins_loaded\')
#5 /.../wp-config.php(89): require_once(\'...\')
#6 /.../wp-load.php(37): require_once(\'...\')
#7 /.../wp-admin/admin.php(31): require_once(\'...\')
#8 /.../wp-admin/tools.php(10): in /.../wp-content/plugins/layers-demo-extension/includes/class-layers-demo-extension.php on line 18
但我不知道如何解决这个问题。。。

1 个回复
最合适的回答,由SO网友:Nathan Johnson 整理而成

在您的get_instance() 方法,您调用self::$instance->construct()self::$instance->__construct(). 而且,没有理由这样调用构造函数;当您调用new self();

结束