cannot redeclare class php

Php: how to resolve «Cannot redeclare class» when there’s no «previously declared»

I want to test the phpDocumentor-alpha, and there’s a problem that some people seems not to have:

Ok, i can avoid that problem with:

But this is just an ugly workaround. I’d like to know if there’s a way to know where the declaration was firt (= which include or whatever).

4 Answers 4

Here’s the simple solution:

Depending on your OS, and how you have constructed the path to your document root, there may be an issue with capitals versus lower case letters in the path name. This often trips me up on large CMS projects such as drupal on Mac OSX

In the file /PEAR/phpDocumentor/vendor/composer/ClassLoader.php, the line 150 is:

symply change it to

In addition to all answers, what you might do is to also consider php declared classes. For example dotnet. When i explicitly define a class named Dotnet for purpose of using Dotnet binaries, it throws an error with the first instance creation of the class.

If you are to use your own modified class other than php presents to you, you should give them distinguishable names.

Not the answer you’re looking for? Browse other questions tagged php or ask your own question.

Linked

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.16.40224

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

PHP Fatal error: Cannot redeclare class

Does anyone know what can cause this problem?

PHP Fatal error: Cannot redeclare class

19 Answers 19

You have a class of the same name declared more than once. Maybe via multiple includes. When including other files you need to use something like

to prevent multiple inclusions. It’s very easy for this to happen, though not always obvious, since you could have a long chain of files being included by one another.

It means you’ve already created a class.

That second Foo would throw the error.

This will happen if we use any of the in built classes in the php library. I used the class name as Directory and I got the same error. If you get error first make sure that the class name you use is not one of the in built classes.

This error might also occur if you define the __construct method more than once.

Читайте также:  пионы легенды и мифы

Sometimes that happens due to some bugs in PHP’s FastCGI.

Try to restart it. At Ubuntu it’s:

I had the same problem while using autoload like follows:

and in other class there was:

The sollution is to keep namespace compatibility, in my example namespace testClassNamespace; in both files.

This error can also occur if you by mistake put a function inside another function.

PHP 5.3 (an I think older versions too) seems to have problem with same name in different cases. So I had this problem when a had the class Login and the interface it implements LogIn. After I renamed LogIn to Log_In the problem got solved.

Just do one thing whenever you include or require filename namely class.login.php. You can include it this way:

This way it never throws an error.

This function will print a stack telling you where it was called from:

Call this function at the top of the file that includes your class.

This should help you find where you class is being included from multiple times in a complex project.

Источник

Fatal error: Cannot redeclare class Database

i have fetal error message say :

Fatal error: Cannot redeclare class Database in C:\wamp\www\pets_new\lib\database.php on line 3

and all connect to database class

3 Answers 3

You include 2 files in a single «run». Think of it like this: All the included files are put together by PHP to create one big script. Every include or require fetches a file, and pastes its content in that one big script.

The two files you are including, both require the same file, which declares the Database class. This means that the big script that PHP generates looks like this:

As you can see class Database is declared twice, hence the error.
For now, a quick fix can be replacing the require(‘database.php’); statements with:

Which checks if that particular file hasn’t been included/required before. If it has been included/required before, PHP won’t require it again.
A more definitive and, IMHO, better solution would be to register an autoloader function/class method, and let that code take care of business.

More on how to register an autoloader can be found in the docs. If you go down this route, you’d probably want to take a look at the coding standards concerning class names and namespaces here. If you conform to those standards, you don’t have to write your own autoloader, and can simply use the universal class loader from Symfony2, or any other framework that subscribes to the PHP-FIG standards (like CodeIgnitor, Zend, Cake. you name it)

Источник

PHP: Cannot redeclare class error after return

I’m trying to protect classes being redeclared by returning based on if class_exists. This is common when creating plugins in WordPress as there may be multiple plugins using the same classes. Using the following trivial example:

Читайте также:  Ингаляция с пульмикортом детям перед сном

The following link demonstrates the problem and the root cause http://www.youtube.com/watch?v=VcNLoDvRLDc

index.php

debug1.php & debug2.php are duplicate files.

Despite having a return statement we still get a fatal error «cannot redeclare class».

I am running PHP version 5.4.4.

So my question is. Is this expected behaviour? If yes, what am I missing. If no, is worrying as I’ve seen this in a few WordPress plugins.

Please note: I can get it to work every time with else wrapping, but this is more code.

2 Answers 2

I can confirm that this is a difference with how xcache is compiling/installing the code. By default PHP install’s classes during compilation and then removes the class code so that it won’t be installed again on execution. This is known as early binding. During this process the install of the second class would fail as the «class exists» and the code will remain intact for execution.

Upon execution PHP will have a second go at installing any remaining classes in the same order as the code this is known as late binding. Any classes which are not reached during execution such as in debug2 are not installed and so do not cause an error. This provides a level of flexibility which allows bad practice such as instantiating a class before it is declared. Or in this case avoiding duplicate includes by checking if the class exists rather than being more diligent with your includes or make your life easier by using an autoload.

Whilst PHP allows this practice xcache have confirmed that they only execute early binding which in turn forces the class redeclaration error. (see: http://xcache.lighttpd.net/ticket/314)

As xcache is used a lot in live environments I would recommend against using a return statement without the else. However, as Jon said autoloading is the way to go, or else wrap the class within an else statement.

Источник

«Fatal error: Cannot redeclare «

I have a function(this is exactly how it appears, from the top of my file):

And for some reason, I keep getting the error:

Fatal error: Cannot redeclare generate_salt() (previously declared in /Applications/MAMP/htdocs/question-air/includes/functions.php:5) in /Applications/MAMP/htdocs/question-air/includes/functions.php on line 13

I cannot figure out why or how such an error could occur. Any ideas?

18 Answers 18

This errors says your function is already defined ; which can mean :

Solution 1

Solution 2

You should include that file (wherein that function exists) only once. So,
instead of : include («functions.php»);
use: include_once(«functions.php»);

Читайте также:  Что такое экстракция в эспрессо

Solution 3

If none of above helps, before function declaration, add a check to avoid re-declaration:

You’re probably including the file functions.php more than once.

you can check first if name of your function isn`t exists or not before you write function By

OR you can change name of function to another name

This answer explains why you shouldn’t use function inside function.

This might help somebody.

I had strange behavor when my *.php.bak (which automaticly was created by notepad) was included in compilation. After I removed all *.php.bak from folder this error was gone. Maybe this will be helpful for someone.

Another possible reason for getting that error is that your function has the same name as another PHP built-in function. For example,

where the checkdate function already exists in PHP.

I don’t like function_exists(‘fun_name’) because it relies on the function name being turned into a string, plus, you have to name it twice. Could easily break with refactoring.

Declare your function as a lambda expression (I haven’t seen this solution mentioned):

Then, at re-execution of said PHP code, the function simply overwrites the previous declaration.

require_once is also useful if the file you’re attempting to include is essential.

I had the same problem. And finally it was a double include. One include in a file named X. And another include in a file named Y. Knowing that in file Y I had include (‘X’)

Since the code you’ve provided does not explicitly include anything, either it is being incldued twice, or (if the script is the entry point for the code) there must be a auto-prepend set up in the webserver config / php.ini or alternatively you’ve got a really obscure extension loaded which defines the function.

means you have already created a class with same name.

That second ExampleReDeclare throw the error.

If your having a WordPress theme problem it could be because although you have renamed the theme in your wp_options table you havn’t renamed the stylesheet. I struggled with this.

I want to add my 2 cent experience that might be helpful for many of you.

If you declare a function inside a loop (for, foreach, while), you will face this error message.

I had this pop up recently where a function was being called prior to its definition in the same file, and it didnt have the returned value assigned to a variable. Adding a var for the return value to be assigned to made the error go away.

You have to deactivate the lite version in order to run the PRO version.

This errors says your function is already defined ; which can mean :

Источник

Образовательный портал