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
Related
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.
In debugging some code I've mistakenly made a call to a member function on a non-object, giving the expected Call to a member function get() on a non-object in /path/to/file.php on line X. I'm accustomed to this, while PHP isn't my favorite language I've been working with it for years. What's confusing me is that when I remove the offending code (I'm using version control and resetting to what should be a working state prior to the bad call), I still get the error. Is it possible for this error to have corrupted an in-memory (I'm using APC object caching). I feel like the answer might be "yes, maybe", however I honestly don't think that explains the problem and am wondering if anyone else has encountered something similar? Is there a method to somehow reset this PHP class object?
Is there a way to catch Call to a member function foo() on a non-object in PHP? It does not sound that serious (as far as fatal errors go), but the shutdown function does not seem to be called at all (PHP 5.3, Debian).
Update:
How to prevent such errors is really not the point. Sure, one should check for null whenever that is an expected possibility, but littering every single object member function reference with error checking code would result in bloated and unreadable code. Hunting down the occassional error based on the logs is fine - the problem is that logs are not very useful for fatal errors. Using a shutdown function would solve that nicely, but I can't get it to work with this specific type of error; which seems strange to me, because it is not an error which would leave the PHP interpreter in a particularly messy state.
I hope this does not sound silly - but you should make sure you know what you are working with. Use instanceof or is_object where you need - or fix the source of the problem - why is that variable not an object in the first place?
I suggest to just ensure, that it is an object. Using methods/functions you can use type hints
public function x (myClass $object) {
$object->foo();
}
else you may use is_object(). At the end such a message sounds like there is a bug within you application, that should be fixed before release, or -- if such a situation can occur by design -- verify the type (is_object() (see above) or !is_null($obj) or something like that) before trying to call something, that does not exist.
A particular code makes the error
include_once "Zend/Mail.php";
$mail->setReplyTo($this->smtp_from,'comp');
The error is:
PHP Fatal error: Call to undefined method Zend_Mail::setReplyTo()
Can you point out possible causes of this?
There was a bug in older versions about that method, for safety use this instead:
$mail->addHeader('Reply-To', $this->smtp_from);
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