I am using WooCommerce on Wordpress 4.7.1. Previously, I was running 4.7.0. When Wordpress automatically upgraded to 4.7.1, I suddenly began seeing checkouts fail to redirect to the "Thank you" page, and the UI showed that a fatal error had occurred.
Turning on debugging revealed the following error:
PHP Fatal error: Cannot redeclare get_cc() (previously declared in .../templates/emails/email-addresses.php:36) in .../templates/emails/email-addresses.php on line 36
I also confirmed that this file is never directly called with require, require_once, include, or include_once. It is, however, called with wc_get_template().
A quick search brought me to this similar question. So I added the following to email-addresses.php:
if (!function_exists('get_cc')) {
function get_cc( $code ) {
... function body
}
}
After making this change, I now receive the following error in the debug log:
PHP Fatal error: Call to undefined function get_cc() in .../templates/emails/email-addresses.php on line 32
This means that, for whatever reason, PHP believes that the function get_cc already exists, but at the same time it is undefined.
I was not the original developer. I took over the project from a contractor who is not available. It does seem clear that this file is heavily modified from the original. I am certain that the value returned by the function must be kept, so I cannot just comment out the call.
Given the information above, what options do I have to either workaround or fix this issue?
It turns out that, unlike a standard function definition in PHP, function definitions wrapped in if (!function_exists('function_name')) { ... } block must precede any call to that function.
In this case, the function definition (line 36) followed the call (line 32). This caused the function to appear to PHP as undefined:
// Not working!
$value = get_cc($code);
if (!function_exists('get_cc')) {
function get_cc( $code ) {
...
}
}
Switching the order so that the function definition came first fixed the issue:
// Working!
if (!function_exists('get_cc')) {
function get_cc( $code ) {
...
}
}
$value = get_cc($code);
As of right now, PHP's documentation does not mention this issue.
Related
One of my projects utilizes the PEAR package HTML_Template_IT plus its extension HTML_Template_ITX. The code worked until a long overdue shift from PHP 5 to PHP 7.4. Now we're getting issues with referencing HTML_Template_ITX() from the extending class.
The implementation in a nutshell looks as sketched below:
<?php
//=== somesite.php ===//
[...]
$template = new siteMainTemplate("article.tpl");
$template->setCurrentBlock("ARTICLE");
$template->setVariable("ARTICLE_HEAD", "some headline");
$template->setVariable("ARTICLE_BODY", "some text");
[...]
$template->parseCurrentBlock();
$template->showSite();
[...]
?>
The class definition extending the PEAR package:
<?php
//=== classdefs.php ===//
require_once "../PEAR/ITX.php";
class siteMainTemplate extends HTML_Template_ITX {
function __construct($pageBody, $pageTitle) {
$this->template = $this->HTML_Template_ITX("../templates/");
[...]
} // ... end of function
function showSite() {
$this->show();
} // ... end of function
} // ... end of class definition
?>
This results in the error "Call to undefined method siteMainTemplate::HTML_Template_ITX()".
The line throwing the error is
$this->template = $this->HTML_Template_ITX("../templates/");
and it looks odd to begin with, but has worked for a very long time. Changing it to something like
$this->template = new HTML_Template_ITX("../templates/");
results in an empty/blank page - well, with errors turned on the following is really happening:
Warning: preg_match_all(): Empty regular expression in /.../PEAR/IT.php on line 1020
Warning: preg_match(): Empty regular expression in /.../PEAR/ITX.php on line 296
Notice: Undefined property: siteMainTemplate::$parseCurrentBlock in /.../classes/classdefs.php on line ...
This clearly points towards a faulty class definition right there.
Can someone advice on what I am doing wrong here?
Edit: Typo corrected in second code snippet - the second function meant to complete the example as "showSite()", not as a second constructor function.
Some more tests revealed the correct line is indeed:
$this->template = new HTML_Template_ITX("../templates/");
After that, the warnings posted in the question were simply covering up further inconsistencies rooted in handling of the "template directory" handed over.
I declared getLesediSimilarPropertyOnShow() function. When I run the application, I get this error below:
Fatal error: Cannot redeclare
GautengPropertyDB::getLesediSimilarPropertyOnShow() in
C:\xampp\htdocs\workspace\ajax-live-search\libs\GautengPropertyDB.php on line 4704
I deleted this entire function thinking about the duplication.
But when I tried to check if the function getLesediSimilarPropertyOnShow() exists by pressing CTRL+F, The Find and Replace Dialog Box displays Not Found in the current document. Meaning the function does not exist. But when I tried to reload the page, the same message persits and the line 4704 is located on comment lines outside of all functions that has nothing to do with the code. I thought may be it was previous error loaded in the cache memory and I cleaned the cache but the same error stands still. But the function does not exist in the file. I don't understand this phenomenon. Can someone please explain this?
At first use IDE like PHPStorm, that show for you where function declared.
Use http://php.net/manual/en/function.function-exists.php for check if function already declared
function createStudentMarks($key)
{ echo "key".$key;exit; }
This is my function.
$StudentMarks=createStudentMarks($key);
And i have called function in same file like this n im getting fatal error. Y is it so can anyone please help me
Cannot redeclare createStudentMarks() (previously declared in /var/www/html/2016/S/Mcd_BuildStudentsMarks.php:85) in /var/www/html/2016/S/Mcd_BuildStudentsMarks.php on line 85 This is the error wat im getting exactly
Most of the times, this is caused by accidentally requireing or includeing a file more than once.
This generally shows bad coding behavior, so first try to find if this is the case or not. Try to eliminate the second and so on requires or includes.
In any case, you can work around it but using include_once or require_once, which will not include the file on second or next calls.
I am getting the following error: PHP Fatal error: Call to undefined function updateRecord()
I know that means that PHP is unable to locate the function I am calling.
I double checked the spelling of the function name and the include file at the top of the page, and both are correct. I am not sure why PHP isn't recognizing the function in the included file.
Here is the code:
include_once('../lib/updatesEngine_lib.php');//include the file with the function
The function call:
updateRecord($jobid);
In updatesEngine_lib.php:
function updateRecord($jobid){//Code//}
I'm at a loss for why this isn't working. Especially since there are other functions in the updateEngine_lib.php file that are structured similarly that are working.
HELP!
Check your spelling for the third time or until you get it work.
There is no magic.
If it says "Call to undefined function" - so it is.
BTW, what if you call this function from updatesEngine_lib.php - does it work?
function getContactActiveEmails($eid)
{
global $db;
if ($eid) {
$sql = "SELECT email FROM activeEmails WHERE id = $eid";
return $db->GetCol($sql);
}
}
I get the error "Cannot redeclare function getContactActiveEmails"
The line number it gives is the last line of the function - the }
All files are being called with require_once. This is the only place in the entire codebase where getContactActiveEmails is being defined. Why is this?
It is very clear from the error your function is defined twice hence you are getting the error.
I would recommend that check if the function is already defined before declaring it.
if (!function_exists('getContactActiveEmails'))
{
function getContactActiveEmails($eid)
{
global $db;
if ($eid) {
$sql = "SELECT email FROM activeEmails WHERE id = $eid";
return $db->GetCol($sql);
}
}
}
solution by #Shakti Singh will work, but keep in mind that you are loosing control of your code - you do not know where is this function declared and what does it return, so I suggest looking for it
Try case insensitive search, many text editors and IDEs search case-sensitive by default and your function can be as well declared as getcontactactiveemails somewhere.
If still no luck make php say something about this function, using Reflection extension
Example usage of reflection:
if(function_exists('getContactActiveEmails')){
$myfunc = new ReflectionFunction('getContactActiveEmails');
echo 'Function is declared in '.$myfunc->getFileName().
' starting from line '.$myfunc->getStartLine().
' to '.$myfunc->getEndLine();
die;
}
More about Reflection
I'm getting the same issue. I have a standard file called adhoc.inc.php which is imported into almost every php file on my site. With no changes being made to any code over night i started seeing the error
[13-Jul-2013 21:19:22 Australia/Sydney] PHP Fatal error: Cannot redeclare checkloggedin() in /Applications/MAMP/htdocs/mycobber/util/adhoc.inc.php on line 4
Initially I only got it in a few files so i just commented out this import and it worked. Suddenly, again no changes, I was getting this in every file i loaded. I figured this wasn't me so I restarted my MAMP servers (apache and mysql) and then it went away.
Has anyone seen this before?
This error occurs if you have your function defined in a loop, since you're trying to define it in each iteration.