Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP class not found but it's included

Tags:

php

I'm including a PHP class with

require_once($ENGINE."/classUser.php"); 

but when the code is executed i receive this error:

Fatal error: Class 'User' not found in C:\xampp\htdocs\WebName\resources\engine\ajax\signup.php on line 12

I still can't figure out what's the problem. I'm 99% sure it's correct.

The "$ENGINE" is correct, and the class is correct too (Netbeans suggests me class methods and variables).

signup.php:

<?php  /* Created on: 13/12/2011  * Author:   *   * Description: User signup procedure.  */  require_once("../settings.php"); require_once($ENGINE."/classUser.php");  $user = new User(); $user->createUser($_POST["username"], $_POST["email"], $_POST["password"]);   ?> 

classUser.php:

<?php  /* Created on: 13/12/2011  * Author:   *   * Description: This class manages users.  */  require_once("settings.php"); require_once($LIBRARY."/cassandraphp/cassandra.php");  class User {      public function createUser($username, $email, $password){         $cassandra = Cassandra::createInstance($CASSANDRASERVER);         $cassandra->set(                 "user.".$username,                 array(                     'ID' => uniqid(),                     'Username' => $username,                     'Email' => $email,                     'Password' => $password                 )         );     }  }  ?> 
like image 797
siannone Avatar asked Dec 13 '11 14:12

siannone


People also ask

What is autoloading class in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

How does PHP autoloading work?

The PHP Autoloader searches recursively in defined directories for class, trait and interface definitions. Without any further configuration the directory in which the requiring file resides will be used as default class path. File names don't need to obey any convention. All files are searched for class definitions.

What is the namespace in PHP?

A namespace is a hierarchically labeled code block holding a regular PHP code. A namespace can contain valid PHP code. Namespace affects following types of code: classes (including abstracts and traits), interfaces, functions, and constants. Namespaces are declared using the namespace keyword.

What is the use of use keyword in PHP?

The use keyword has two purposes: it tells a class to inherit a trait and it gives an alias to a namespace.


1 Answers

Check to make sure your environment isn't being picky about your opening tags. My configuration requires:

<?php 

If i try to use:

<? 

Then I get the same error as you.

like image 117
jarederaj Avatar answered Sep 28 '22 05:09

jarederaj