Fatal error: Cannot redeclare PHPUnit\Framework\assertArrayHasKey() - php

I need help with this error when try to run phpunit tests in VS code terminal at address: D:\xampp\htdocs\coscProj>
Fatal error: Cannot redeclare PHPUnit\Framework\assertArrayHasKey() (previously declared in phar://D:/xampp/php/phpunit.phar/phpunit/Framework/Assert/Functions.php:80) in D:\xampp\htdocs\coscProj\vendor\phpunit\phpunit\src\Framework\Assert\Functions.php on line 79
As I tried to install phpunit with multiple ways, I am not sure is that the cause of this error or not.

This error tells you that your function is already defined. This usually happens when a file is included/required several times, or the function with the same name is declared in several times. You can wrap function_exists around your function definition, like
if (!function_exists("somefunction")) {
function somefunction() {}
}
But that might be a naive approach. You will first need to make sure that you do not require the same thing several times. If only a single function is duplicated, then you can add the logic above. require_once and include_once are keywords that spring to mind when this kind of issue happens.

Related

Class already declared in PHP and IP.Content?

I am including a file with a class GroupManager which is eval'd with IP.content. I have received the error that the class is already defined. If I un-include, I receive no errors. If I rename the class, I receive no errors (however I am certain this class does not exist elsewhere.) I tried the following in case there were multiple includes (I was personally using include_once...):
if( !class_exists('GroupManager') ) {
class GroupManager {
}
}
I still receive the error:
Cannot redeclare class GroupManager in ... GroupManager.php on line 37
Obviously there is no line 37 in my 4-line file.
If I remove the class declaration, and replace it with an echo, I receive no error and it echos fine, which means the class does not exist at that point.
if( !class_exists('GroupManager') ) {
echo "Class does not exist???";
}
I am unsure how to even debug this.
I found the issue, and I should have caught it a lot sooner... Another administrator had included this file in a hook. The hook is apparently run on every page, even though every page does not require the hook... Thus it was not my thought to check the hooks.
If anyone has weird include issues with IPB, make sure you grep for the includes/requires. It'll save you a lot of hassle.
Something like this should give you an idea of where the file is being included:
grep -r include_name.php .
This will check all files recursively from the current dir.

PHP - Fatal error: Cannot redeclare class Config in /path/to/Config.php on line 44

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.

Redeclare function works on old server but not new

I'm migrating a PHP 5.2.x application to a new 5.2.x server. The old server actually started as a PHP 4.0 server many years ago and was upgrade to PHP 5.2 over time. One of our modules has a function that gets redeclared if this module is used more than once. We can easily fix this, but we're perplexed at how it ever could have worked.
On the new server it will fail with an expected:
Fatal error: Cannot redeclare function
The problem is that on the old server it was always re-declaring the function! Is there a PHP setting or special usage being used here that makes it work on one server but not another?
Thank you!
Edit Still trying to pour through how this is possible. The site FATAL errors but has execution after that point of error.
Redeclaring functions is consider a error.
Maybe you guys can use "rename function".
http://es.php.net/manual/en/function.rename-function.php
if(function_exist("foo")){
rename_function('foo', 'old_foo' );
function foo(){
/*...*/
}
}
Another idea is to rewrite code to do this
$foo = function(){ /* something */.... };
So the next time you want to redefine $foo(), you do
$foo = function(){ /* something else */.... };
I am unable to reproduce your description, PHP 4 does not allow you to redeclare functions:
echo PHP_VERSION;
function foo() {}
function foo() {}
Demo/Output:
4.4.9
Fatal error: Cannot redeclare foo() (previously declared in /homepages/26/d94605010/htdocs/lz/writecodeonline.com/php4/index.php(138) : eval()'d code:2) in /homepages/26/d94605010/htdocs/lz/writecodeonline.com/php4/index.php(138) : eval()'d code on line 3
You must mix things here, so better find out more about facts when debugging, not guessing (yes I know can be hard sometimes, but facts help when debugging, aim for them).
And if it's a fatal error, your script ends. You can add a shutdown callback function to further debug your issue, see a related question:
PHP : Custom error handler - handling parse & fatal errors

How to debug a class redefinition in PHP?

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.

Fatal error: Cannot redeclare class Customer

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

Categories