我知道有很多人遇到过这种问题,但我没有找到任何对我有帮助的解决方案。
我有一个简单的Wordpress插件,其中包含一个主插件文件和另外两个文件。
/myplugin.php
/framework/template/content.php
/framework/pagination/pagination.php
myplugin.php简单地回显了content.php文件。 pagination.php是一个名为'Pagination'的类 - 它是一个github项目,你可以在这里看到它的内容。 Flexible-PHP-Pagination
当我尝试在 content.php 文件中使用此类时,会出现此问题。我可以毫无问题地包含该类,但只要我想在Pagination类中创建对象,它就会返回致命错误:
Fatal error: Uncaught Error: Class 'Pagination' not found in /var/www/.../wp-content/plugins/myplugin/framework/template/content.php:382
我创建对象的方式是:
$nav = new Pagination($max, $total, $page, $maxNum);
(Note: all variables have proper values)
第382行是我创建$ nav变量的行,而不是我包含文件的地方(第3行的那些)。任何想法可能是什么问题?
更新(现在工作) 这是我之前导入文件的方式:
include(plugins_url('', __FILE__ ) . '/..../myfile.php');
显然,不应该这样做,但这样做:
require(plugin_dir_path( __FILE__) . '..../myfile.php'); /* Notice missing slash */
现在可行。
答案 0 :(得分:0)
您应该使用以下方法加载课程:
add_action( 'plugins_loaded', array( 'someClassy', 'init' ));
class someClassy {
public static function init() {
$class = __CLASS__;
new $class;
}
public function __construct() {
//construct what you see fit here...
}
//etc...
}
或
add_shortcode( 'baztag', array( My_Plugin::get_instance(), 'foo' ) );
class My_Plugin {
private $var = 'foo';
protected static $instance = NULL;
public static function get_instance() {
// create an object
NULL === self::$instance and self::$instance = new self;
return self::$instance; // return the object
}
public function foo() {
return $this->var; // never echo or print in a shortcode!
}
}