可能重复:
Derived class defined later in the same file “does not exist”?
有没有人有轻微的想法为什么我得到一个致命错误:尝试在下面的if语句中初始化它时找不到类'PublicacionController'?
--PublicacionController.php--
<?php
/*Some random includes, those are
right as far as Im concerned*/
//AJAX call
if(!empty($_POST)){
if($_POST['call']=='nuevaPublicacion'){
$pc = new PublicacionController();
$pc->nuevaPublicacion($_POST);
exit;
}
}
class PublicacionController extends Controller{/* STUFF*/}
?>
这是一个单独的文件。我从AJAX调用中调用控制器(如果它与某些事情有关,则不知道)。
我正在运行一个标准的Amazon Ec2实例,使用Amazon Linux和来自repos的默认https和PHP版本(我认为使用相同的Fedora)。
答案 0 :(得分:6)
应在实例化之前定义PHP类,请参阅the "new" section of the PHP OO documentation。
实现这一目标的简单方法是首先声明类,然后是主代码:
--PublicacionController.php--
<?php
/*Some random includes, those are
right as far as I'm concerned*/
class PublicacionController extends Controller{/* STUFF*/}
//AJAX call
if(!empty($_POST)){
if($_POST['call']=='nuevaPublicacion'){
$pc = new PublicacionController();
$pc->nuevaPublicacion($_POST);
exit;
}
}
?>
答案 1 :(得分:1)
它是一个AJAX调用的事实与它没有任何关系,但调用代码在类声明之上的事实确实如此。
交换代码,甚至更好,将其移动到单独的文件中。
答案 2 :(得分:1)
这是PHP ERROR
,请参阅:Derived class defined later in the same file "does not exist"?
如果你跑
if (! empty($_POST)) {
if ($_POST['call'] == 'nuevaPublicacion') {
$pc = new PublicacionController();
$pc->nuevaPublicacion($_POST);
exit();
}
}
class Controller {
function nuevaPublicacion($array) {
}
}
class PublicacionController extends Controller {/* STUFF*/
上面的代码可以很好地工作,通过外部文件包含Controller
它将开始生成错误。
建议在您使用它们之前声明所有类,特别是在处理继承时