3

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).

Community
  • 1
  • 1
Hito_kun
  • 962
  • 3
  • 13
  • 26

3 Answers3

8

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

?>
hakre
  • 193,403
  • 52
  • 435
  • 836
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 2
    -1. PHP parses the whole file first. – Ja͢ck Oct 01 '12 at 15:05
  • Please read the cited reference, the official PHP documentation. `Classes should be defined before instantiation (and in some cases this is a requirement). `. – Fenton Oct 01 '12 at 15:09
  • 2
    @Sohnee: The reference is missing that *defined before* actually means *written in file before*. However I'm with you, that in some cases, it must be done that way. But I must admit, I'm not sure if this is the case here (but OP should have tested that anyway, so not a bad suggestion). – hakre Oct 01 '12 at 15:20
  • Granted. It should and doing so would likely solve the issue, though it's not the root cause as mentioned in the possible dupe. I would like to revoke the DV, could you make a minor edit pls? – Ja͢ck Oct 01 '12 at 15:25
  • This one seems to work, but now controller.class.php (which Im including at the beggining of the file via require_once(realpath(dirname(__ FILE __).'/../..') .'/library/controller.class.php'); )is doing some weird stuff... I'm gonna check it out first – Hito_kun Oct 01 '12 at 15:28
  • Checked... probably this will do the trick. Now I have to work a little bit with some missed includes Im skipping now since Im calling the controller directly and not through the dispatcher... – Hito_kun Oct 01 '12 at 15:50
  • 2
    For reference...i've found that if you *conditionally* define a class, or you define it in another file, it has to be defined before you use it (or it can be autoloadable, but autoloading is just on-demand definition). If it's always defined in that file, then it can come after. – cHao Oct 01 '12 at 16:16
  • Apparently, this also applies to child classes. I had a class extending another class, defined before the parent class, and I got the class not found error. I am using PHP 5.5.9-1. I.E. child classes must, apparently, be defined after parent classes. Maybe that's obvious, but it surprised me. – Buttle Butkus Feb 23 '15 at 03:39
1

The fact that it is an AJAX call doesn't have anything to do with it, but the fact that the calling code is above the class declaration does.

Swap the code, or even better, move it to a separate file.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • It's easy to write a counter-example. Additionally, class autoloading even allows to instantiate a class that has not even been defined yet as far as the interpreter is concerned. Am I missing something? – Álvaro González Oct 01 '12 at 15:10
  • That's true, but in that case, the auto-loader is already active, and it will search the class in *an other* file. In this case, you're trying to instantiate a class that isn't defined yet, and no auto-loader can help, because the file is already being parsed. – GolezTrol Oct 01 '12 at 15:15
  • 1
    Sohnee's answer contains more information. I could update my answer and add that as well, but that's a little pointless, I guess. The point is the same after all. – GolezTrol Oct 01 '12 at 15:17
  • Now only to convince a bunch of downvoters. ;) – GolezTrol Oct 01 '12 at 16:03
1

This is a PHP ERROR see : Derived class defined later in the same file "does not exist"?

If you run

if (! empty($_POST)) {
    if ($_POST['call'] == 'nuevaPublicacion') {
        $pc = new PublicacionController();
        $pc->nuevaPublicacion($_POST);
        exit();
    }
}

class Controller {
    function nuevaPublicacion($array) {
    }
}
class PublicacionController extends Controller {/* STUFF*/

The code above would work fine be the moment Controller is included via external file it would begin to generate error.

Advice declare all your classes before you use them for now especially when dealing with inheritance

Community
  • 1
  • 1
Baba
  • 94,024
  • 28
  • 166
  • 217
  • 1
    Where's the link to that bug? – Ja͢ck Oct 01 '12 at 15:08
  • @Jack hold on before you start down voting let me get you more information – Baba Oct 01 '12 at 15:09
  • @Jack see http://stackoverflow.com/questions/12617188/derived-class-defined-later-in-the-same-file-does-not-exist/12617996#12617996 and see my investigation ... http://stackoverflow.com/a/12617996/1226894 – Baba Oct 01 '12 at 15:13
  • 1
    Interesting. I'm going to assume that this will be a "will not fix" on bugs.php.net? Btw, it wasn't me who DV your answer ;/ – Ja͢ck Oct 01 '12 at 15:17
  • @jack .. i really appreciate .. am still looking for the link anyway – Baba Oct 01 '12 at 15:19
  • the function nuevaPublicacion is on PublicacionController, not in controller class... does it changes anything of your explanation? – Hito_kun Oct 01 '12 at 15:32
  • @Hito_kun it does not matter where `nuevaPublicacion` is located the main issue is `PublicacionController extends Controller` when Controller is in another PHP file .. – Baba Oct 01 '12 at 15:35