Lienau
Lienau

Reputation: 1353

PHP class causing error

I'm getting this really bad error. I tried copying the example classes off PHP.net. The class worked but I cant seem to get it to include right. My index file includes the users.class.php and then the content.php which has the call to the class.

Error:

Fatal error: Class 'A' not found in X:\xxxxx\xxxx\xxxxx\content.php on line 2

index.php:

<?php
   require('users.class.php');
   $a = new A();
   require('content.php');
?>

content.php:

<?php
   echo $a->foo();
?>

users.class.php:

<?php
   class A
   {
      function foo()
      {
         return 'hello world';
      }
   }
?>

Upvotes: 0

Views: 62

Answers (2)

Gabriel Sosa
Gabriel Sosa

Reputation: 7956

first: I ran your code and works

second: the error make no sense since content.php doesn't not contains the declaration of the class "A", in any case the error it should say that the class was not found in the "index.php" file.

Please check for hidden chars and try again

Upvotes: 0

Brian Driscoll
Brian Driscoll

Reputation: 19635

hmm... my guess is that the line

echo $a->foo();

is being executed before the preprocessor has fully read in users.class.php.

try adding this line to content.php:

require_once("users.class.php");

above the echo... line.

Also, change your index.php require to require_once as well. This will ensure that your class is read in before code is executed, and you won't get any errors saying that the file has already been included.

Upvotes: 1

Related Questions