I have a class file: class_xx.php. And then a function file: function_xxx.php
In my function_xxx.php:
require_once('class_xx.php')
... // after few next lines
$object = new class_xx1($arg1, $arg2);
But it gives me:
Fatal error: Class 'class_xx1' not found in "some_path" on line "1XX3"
[sorry I can't exposed the codes yet], any idea why I included the file > require_once with no error, but it gives me "Class not found error"??
Seemingly, the class_xx.php does not correctly declare the class_xx1 class. Review your code and watch for typos. Put some sort of debug line like echo "hello; in the required file if you want to be sure that it is being included correctly.
Chances are you misspelled the class declaration or something to that avail. You will what to double-check that your spell it the exact same way, with the same casing.
If you are developing on a secondary sever, you might have not transferred the completed class_xxx.php file over and just a blank file, in which case PHP would be including a blank file.
By the way, you forgot a semi-colon after the require_once
Related
This is a WordPress local installation that I am trying to work with. I have not written a single line of this code myself. I don't understand what this error means:
Fatal error: Cannot redeclare class Config in /Applications/XAMPP/xamppfiles/lib/php/Config.php on line 44
Line 44 reads as follows:
class Config {
My guess is that a Config class has either already been declared elsewhere, or that this file is being executed for the second time.
That usually happens when you declare a class more than once in a page -- maybe via multiple includes.
To avoid this, use require_once instead. If you use require_once PHP will check if the file has already been included, and if so, not include (require) it again.
Say, for example, you have the following code:
<?php
class foo {
# code
}
... more code ...
class foo { // trying to re-declare
#code
}
In this case, PHP will throw a fatal error similar to the one below:
Fatal error: Cannot redeclare class foo in /path/to/script.php on line 7
In this case it's very simple -- simply find the 7th line of your code and remove the class declaration from there.
Alternativey, to make sure you don't try to re-declare classes, you can use the handy class_exists() function:
if(!class_exists('foo')) {
class foo {
# code
}
}
The best approach, of course, would be to organize all the configurations in one single file called config.php and then require_once it everywhere. That way, you can be sure that it will be included only once.
As for debugging the error, you could use debug_print_backtrace().
It's possible that the theme you are using refers to a file called config.php. If so use the following steps.
Try to find the config.php file and change it's name to configuration.php.
Find the files where they use config.php in the code and change it to configuration.php.
I know this has been answered before and why this is caused. However, in my case, the problem is happening only on MacOS Lion (10.7) which is on php 5.3.6. The same code base is running on my windows 7 machine which is on php 5.3.8.
I have used require_once all over the place. The code fragment that seems to be causing the problem is:
class DbBase
{
...
}
in a file that is included from multiple files. However the error disappears if I wrap the class declaration inside of:
if (class_exists('DbBase') != true)
{
class DbBase
{
...
}
}
I have this scenario:
File DBBase.php:
defines Class DBBase
File A_DB.php:
require_once("DBBase.php")
File B_DB.php:
require_once("DBBase.php")
File foo.php:
require_once("A_DB.php")
require_once("B_DB.php)
So the file DBBase.php does get included twice. Does it?
Any insight is appreciated.
I was having the same error. However, it wasn't related to 'declare a single class multiple times'. It was because I've made a backup of the class like this:
class.shipping.php
class.shipping_092512_bk.php
Then, I uploaded the backup file to the server. That caused the application to fail due to the two classes. So, I renamed the backup to something else without .php file extension 'class.shipping_092512.bk'. Problem solved.
This error occurs when you declare a single class multiple times instead of once ; The code below would throw that error.
class phpp{}
class phpp{}
It is unlikely that the behaviour is not the same on 5.3.6 and 5.3.8 as there haven't been any significant changes in classes and objects.
You should check for duplicated codes in the files you are requiring once. . .
EDIT :
The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.
So in your case it is not being included multiple times, whereas it would if you use require or include.
However you should restructure your entire code to prevent class redeclaration's.
class lol{}
if(!class_exists("lol")){
class lol{
function ll (){
return "ss";
}
}
}
echo lol::ll();
You wouldnt be able to access that ^^ , since its still a redeclaration.
I have the following error
Fatal error: Cannot redeclare class Database in /home/content/63/8026363/html/include/user_database.php on line 5
That links to the line
class Database
But I have not redeclared this anywhere I can see.
I've checked all the include files and still can't see it.
I have now changed the name to user_database1.php which is DEFINITELY only included once in my WHOLE system and I am still getting the same message!
This only occurs in my root/admin directory.
When I moved the file it occurs into the root directory and updated the include files from ../file.php to just file.php, it worked perfectly.
I can't understand why having the file.php in the /admin directory and using ../ to include files isn't working!
Can anyone offer any experience of this? Or a potential fix.
I'll provide some code from the top of the file in question..
<?php
include("../include/session.php");
include("../include/admin_database.php");
Clearly this is the problem but I can't understand why!
Hope someone can help !
(question has been updated significantly since a lot of the answers below were submitted)
If you included the file two times, theen it gets re-declared. Use include_once() instead to prevent that easily.
If you are unsure where that class was originally declared, you can make use of the Reflection API to get the name of the file and the line of code:
$class = 'Database';
if(class_exists($class))
{
$oRefl = new ReflectionClass($class);
$message = sprintf('Class %s already defined in %s on line %s.', $class, $oRefl->getFileName, $oRefl->getStartLine);
throw new DomainException($message);
}
Place that before the line where you define the class Database to find out which file was originally defining the class.
Check if php already defines a file called Database.php.
Maybe you are using some framework or library which does.
You probably have included your database class more than once, a quick fix for this is to add something like this to the top of your class:
if(!class_exists('Database')){
class Database{
// so on
}
This ensures that your class is only defined once.
I'm getting a PHP Fatal error: Cannot redeclare class Foo in /directory/ on line 20 error, but I have no idea where it's coming from. I'm always using require_once for this class file, and I'm not sure how to debug it. Can I get some kind of inclusion stack trace somehow? I'm running PHP 5, so case sensitivity such as descriped here should not be a problem: http://www.php.net/manual/en/function.include-once.php.
Use debug_backtrace in file where is class declared, but before it's declaration
Another approach is to rename the class that was redefined (also rename the file containing the class) and then fix all class-not-found errors you get from that. That should lead you to the code that is causing the redefinition. In my case it was a class_alias statement that was causing the problem.
I'm getting this error:
Fatal error: Cannot redeclare class Customer
Then I added the following code:
if (!class_exists('Customer')) {
include('include/customer.class.php');
}
Why do I still get that error?
I have a file (file1.php) which has the Customer() class declared.
In file1.php I make an ajax call to file2.php
In file2.php I declare the Customer() class again.
In file2.php there is only 1 declaration of Customer() class.
Check if your server runs opcode cacher like APC - that's the cause of an error. I've runned into it recently.
Clearly due to the fact I issue:
if (!class_exists('Customer')) {
The class doesn't exist so the class itself is somehow duplicating itself.
I use this class in numerous other pages in the application without a problem.
I simply removed the whole thing:
if (!class_exists('Customer')) {
include('include/customer.class.php');
}
And it somehow worked which is preplexing!
If the class existed, the class file should never be included...
It doesn't exist therefore, the class is being included.
Once included, it says it's already included...
Very, very odd...
Well, it's working now... I guess i'll leave it be...
Use include_once(). If that still gives you an error, the problem is that you are declaring the class more than once in the file "include/customer.class.php"
http://php.net/include_once
The errors could be caused by a class defined multiple times, for example:
class Foo() {}
class Foo() {} // Fatal error
If you are not sure how many times your class will be included you can two things:
Use include_once() or require_once() in order to be sure that that file is required "once" only.
Write that code you provided every time you are including that file:
if (!class_exists('Customer')) {
include('include/customer.class.php');
}
I'd prefer the first though.
Your problem is the one described above. There must be a place where the class is declared multiple times. Without any code is hard to tell where.
Here's some references:
include_once()
require_once()
PHP: The Basics