PHP:_调用工作不正常
我在WordPress中使用magicPHP:_调用工作不正常,php,wordpress,callback,naming,magic-methods,Php,Wordpress,Callback,Naming,Magic Methods,我在WordPress中使用magic\uu call函数创建名为API\u Widgets的类时遇到问题。如果我简单地将文件重命名为derp.php,将类重命名为API\u derp,那么它就可以正常工作 下面的示例已经删除了与此问题无关的所有内容(因此,如果存在第三个代码块中指定的特定致命错误以外的任何错误,请忽略它) 请记住,我知道core.php或API类的\u调用很有效,因为重命名widgets.php或调用另一个类都很有效 core.php: class API { fun
\uu call
函数创建名为API\u Widgets
的类时遇到问题。如果我简单地将文件重命名为derp.php
,将类重命名为API\u derp
,那么它就可以正常工作
下面的示例已经删除了与此问题无关的所有内容(因此,如果存在第三个代码块中指定的特定致命错误以外的任何错误,请忽略它)
请记住,我知道core.php
或API
类的\u调用
很有效,因为重命名widgets.php
或调用另一个类都很有效
core.php:
class API {
function __call( $method, $args ) {
$rmethod = "API_{$method}";
if ( !class_exists( $rmethod ) ) {
$lmethod = strtolower( $method );
require_once( "{$lmethod}.php" );
}
return new $rmethod( $args );
}
}
widgets.php:
class API_Widgets {
private $widgets = array();
function Add( $widgets ) {
if ( is_array( $widgets ) ) {
foreach ( $widgets as $widget ) {
if ( !in_array( $widget, $this->widgets ) )
$this->widgets[] = $widget;
}
} else
$this->widgets[] = $widgets;
}
}
api.php:
$api = new API();
$widgets = $api->Widgets(); // Fatal error: Class 'API_Widgets' not found in /home4/goldencr/public_html/wp-content/plugins/minecraft-api/api/core.php on line 25
//$widgets->Add( 'some_widget' );
引自评论: 虽然没有暗示您的问题,但似乎您实际上可能没有包括
widgets.php
。尝试使用绝对路径来修复此问题:
require_once( __DIR__.DIRECTORY_SEPARATOR.$lmethod.".php" );
我运行它时没有发现错误:您是否也看到
include
file\u not\u found problem?您可以尝试使用绝对路径,如\uuuu DIR\uuu.$lmethod..php“
。绝对路径解决了我的问题。我在其他地方使用绝对路径,我只是不知道这里没有使用它。回答一下,我就接受这个答案@过路人