在同一文件中找不到类 [英] Class not found in the same file

查看:145
本文介绍了在同一文件中找不到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

稍后在同一文件中定义的派生类不存在


有没有人有点儿想法为什么我得到致命错误:类'PublicacionController'没有找到时尝试在下面的if语句初始化它? / p>

   -  PublicacionController.php-- 
<?php
/ *有些随机包括
如果我有相关的* /

// AJAX调用
if(!empty($ _ POST)){
if($ _ POST ['call' ] =='nuevaPublicacion'){
$ pc = new PublicacionController();
$ pc-> nuevaPublicacion($ _ POST);
exit;
}
}

类PublicacionController extends Controller {/ * STUFF * /}
?>

这是一个单一文件。



我运行一个标准的Amazon EC2实例,使用Amazon Linux和默认的PHP类应该在实例化之前进行定义,请参见下面的示例:PHP实现之前应该定义PHP类,请参阅下面的代码:http://blog.csdn.net/detail.php?title= PHP OO文档的新部分



一个简单的方法是首先声明类,然后声明主代码:

   -  PublicacionController.php-- 
<?php
/ *一些随机包括,那些是
就我的意思* /

class PublicacionController extends Controller {/ * STUFF * /}

// AJAX调用
if(!empty($ _ POST)){
if($ _ POST ['call '] =='nuevaPublicacion'){
$ pc = new PublicacionController();
$ pc-> nuevaPublicacion($ _ POST);
exit;
}
}

?>


Possible Duplicate:
Derived class defined later in the same file “does not exist”?

Does anyone has the slighties idea why I'm getting a Fatal Error: Class 'PublicacionController' not found when trying to initialize it in the if statement below ?

--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*/}
?>

It is a single file. Im calling the controller from an AJAX call (dunno if it has something to do with).

I'm running a standard Amazon Ec2 instance, with Amazon Linux and the default https and PHP versions from the repos (the same Fedora uses I think).

解决方案

PHP classes should be defined before instantiation, see the "new" section of the PHP OO documentation.

An easy way to achieve that is by first declaring the classes and then the main code:

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

?>

这篇关于在同一文件中找不到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆