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.
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 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.
I am trying to setup OData connection to MySQL database .. when I run the MySQLConnector.php , I am getting the above error.. can someone please direct me towards a solution.
The statement that gives the error is:
public function getEntityTypeName($entity)
{
return \Inflector::singularize($entity);
}
Following is the code in Inflector.php:
function singularize($word) {
$_this =& Inflector::getInstance();
if (isset($_this->_singularized[$word])) {
return $_this->_singularized[$word];
}
Please let me know if you would need any further information. Thanks in advance.
The short answer is: you need to update both of them. It looks like you have an older Inflector which is relying on deprecated PHP behaviour, and it's likely that your MySQLConnector.php is also old. If you don't update, you'll likely hit further problems.
In this case, PHP is complaining that you're using a static call to a method which is missing the "static" keyword. It's very likely that this message is a warning not an error, so it probably isn't causing whatever end problem you're experiencing. If you really want to address this message, you can just write static public function singularize($word) { instead, but like I said you will have more problems.
I am creating a function that converts a users initials (STRING) to their userid (INT)
problem is when I call the function I get a call to undefined func error because the below declared function is no where to be found in the Source!
// connect to database -- this works, I checked it
function convertRadInitToRadID($radInits){
$sqlGetRadID="SELECT id FROM sched_roster WHERE radInitials == '".$radInits."'";
$resultGetRadID=mysql_query($sqlGetRadID);
$radID=mysql_result($resultGetRadID,0);
return $radID;
}
...I then create and array ($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter) of user initials, it works with no errors I tested it independently
$randKey=rand(0,(count($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter)-1));
$randRad=$radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter[$randKey];
$randAssignedRadID=convertRadInitToRadID($randRad); // call to undefined function error here
when I view source the function definition code (where I define the function) is nowhere to be seen in the source. Very strange. I tried placing it around different areas of the script, same error. The function definition is declared appropriately and is wrapped in .
Very strange. The function just doesn't appear. Quotations, syntax, semi-colons, etc are all spot on.
No syntax errors. Advice?
I Strongly agree with Answer #1.
In addition a usual problems occur in php if you are defining function after calling it. i.e. your calling code is before function defination then it will not run and will give an error of undefined function.
You can create a class then define this function in that class and on the time of calling you can call that function with help of $this->function(args)
I think this will resolve your problem in mean while i am trying to run your code on my machine, lets see what happen
May be your function is a method of some class. So, if it is, you should use it in another way:
MyClass::convertRadInitToRadID($radInits) // if static method
or like this
$class = new MyClass();
$class ->convertRadInitToRadID($radInits)
Trying to make sense of your question... Are you trying to call the function using JavaScript? If so, remember that JavaScript is run on the browser, and PHP is run on the server (and this is why when you "view source" you don't see the function anywhere). To send data back from JavaScript to PHP you should use AJAX.
I found the answer: I was using Jquery UI tabs.... there must be a conflict with tabs. When I run the code without tabs there is no issue.
Thanks for the '==' fix.. appreciate it. my bad
thanks for reminding me about the 80 char varname code limit
im getting this error message
Fatal error: Cannot redeclare get_db_conn() (previously declared in `/home/maxer/domains/x/public_html/xmasapp/dbfuncs.php:21) in /home/maxer/domains/x/public_html/xmasapp/dbfuncs.php on line 24`
this is the code
function get_db_conn() {
$conn = mysql_connect($GLOBALS['db_ip'], $GLOBALS['db_user'], $GLOBALS['db_pass']);
mysql_select_db($GLOBALS['db_name'], $conn);
return $conn;
}
line 21 refers to
$conn = mysql_connect($GLOBALS['db_ip'], $GLOBALS['db_user'], $GLOBALS['db_pass']);
line 24 is the closing curly bracket of the function
the code worked fine until I tried to clean my code up, I ripped most of the "view" code out and put it into separate files but didn't change any logic
You are most likely including a file twice or including two files that include the same file each.
You can prevent this by using include_once() or setting up a better structure of what you include when.
EDIT
Try this and see if you see an error in your include setup.
echo "<pre>";
print_r(get_included_files());
echo "</pre>";
Somewhere you're including a file twice or some two files has a definition of your function.
Is this in an includes file? Is the includes file getting included more than once?
It's complaining because the get_db_conn is defined more than once, and most likely it's getting included multiple times unless you have that function in two different places.
Your error message says:
Cannot redeclare get_db_conn() (previously declared in [...]/dbfuncs.php:21) in [...]/dbfuncs.php on line 24
You have a function named get_db_conn() that you are declaring multiple times. Is your dbfuncs.php file including itself?
It's possible you're include()ing the same file more than once. For this reason I like to use include_once() whenever possible.
You declare two functions with identical names (both are called get_db_conn())