Fatal error: Cannot redeclare close() (previously declared in
E:\wamp1\wamp\www\sample.php:1) in E:\wamp1\wamp\www\sample.php on
line 9
function close($tb,$lr) {
echo '#close {
'.$tb.':-2.5%;
'.$lr.':-1.5%;
position:absolute;
cursor:pointer;
}';
}
close($close_tb, $close_lr);
When I run this script it says cannot redeclare close()(previously decalred in line 1) in line 9.
Even on error the values associate with $close_tb and $closr_lr are passed to the css.
What wrong am doing here? am just calling the function in line 9. Any help?
This means you already have a function called close().
Maybe you're including the same file twice or had a freak copypasta accident. Check your code for any double functions.
The fact that your script still works as expected likely means you have the exact same function twice.
Your error code means that you are defining the function close twice. The code you have provided does not illustrate this, however, you must have either included the file more than once, or may have duplicated your code.
Go through your code and ensure you are not including the function definition more than once.
Related
I have made a public function for getting, and showing a user's infraction info. When I put it on a page, it only shows what I have in the function, and not any of the other content on the page. It only shows the table headers, and none of the data. I get this error:
Fatal error: Call to a member function query() on a non-object in /Applications/AMPPS/www/classes/user.php on line 108
Also, I have other functions from the same class that work fine.
Here is the code for the function link (sorry about pastebining it, it was really long)
Your $db object is null or can't be accessed. Your line 108 error does not match up with your code you have pasted and you don't have the code where you are creating your database object to see what may be wrong there.
The error message seems to indicate that your "$db" is not set to an object. Make sure its initialized properly.
The function never initializes the $db variable. If it's a class property, it should be $this->db or self::$db. If it's a global variable, you need to put global $db; at the beginning of the function.
Considering the following PHP class:
class someObject {
public function broken(){
return isset($this->something()) ? 'worked' : 'didnt';
}
public function something(){
return true;
}
public function notBroken(){
print('worked');
}
}
Let's say I now do:
$obj= new someObject();
$obj->broken();
Considering you can't pass a function call to isset(), (it's by-reference), I expect this to fail with a fatal error: PHP Fatal error: Can't use method return value in write context This is fine, and expected.
However, let's say I now do:
$obj= new someObject();
$obj->notBroken();
Considering I'm not hitting the broken() anywhere in this execution, and the error in broken() is a Fatal Error (and not a Parse error), I wouldn't expect the normal output of "worked". FALSE! It still generates the Fatal Error.
Question:
Aside from just not writing code that has errors, are there any other errors that are not Parse Errors but still trigger a runtime error? I only know about: PHP Fatal error: Can't use method return value in write context. Is there any way to detect these errors?
Is there a special name for this type of error?
The reason for this specific behaviour is probably that isset() is a language construct and not a normal function that gets interpreted at runtime. So it stands to reason this is kind of a parse error.
I have no deep insight in this though, and I don't know whether this class of errors has a specific name.
These are "compile errors", thrown by the compiler when it encounters a syntactically valid but "uncompilable" construct. Go to http://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_compile.c and search for "E_COMPILE_ERROR" - there are quite a few.
I am getting this error for re-declaring saveorder() however, I don't think I am?!?
Cannot redeclare saveorder() (previously declared in :10) on line 71
8.function saveOrder()
9.{
10. include 'tables.php';
11. $orderId = 0;
12. $shippingCost = 5;
...
68. }
69. echo $orderId;
70. return $orderId;
71. }
You could be including the file that contains the function more than once:
include 'file.php';
include 'file2.php';
file.php:
include 'file2.php';
Cannot redeclare saveorder() (previously declared in :10) on line 71
Either use include_once or require_once to make sure it doesn't happen (this can cause problems if you try to include it twice in two separate locations (like first in a file, then later inside a function for some reason, the second one will not work if you include the _once part).
You must be including the current file (where the lines are from) multiple times.
An easy fix is using
if (!function_exists('saveOrder')) {
function saveOrder() {...}
}
However, I recommend creating a new functions.php file, including in only once, and placing all functions there.
Either tables.php contains a function also called saveOrder(), or the file you posted actually IS tables.php. PHP can't have 2 functions with the same name in the same namespace.
this could also be caused be saveorder() being delcared inside another function that is called multiple times.
eg.
function func1()
{
function saveorder()
{
echo 'x';
}
saveorder();
}
for ($i=0;$i<2;++$i)
func1();
I think this must be a bug in PHP since where you have ":10" I get all sort of strange symbols that do not occur anywhere in my code - ie. one time it might be ":0", next time ":196870" and the next time "!qhsu89s3". I also find that if I wait around a bit before refreshing then it normally sorts itself out.
Not too encouraging I have to say, but I presume a problem with PHP on Windows.
I've been implementing a certain plugin (dtabs) on my page in Wordpress but after upgrading to the latest version, I found that I now have an error the 2nd time I call the main function called dtab_list_tabs().
The way it works is, the plugin gets include_once'd but the main function is called however many times you want to place tabs in your layout. I have 2 such calls to dtab_list_tabs().
Now, the problem is, for whatever reason the developer decided to include another function directly inside dtab_list_tabs() called current_tab(). Because it's declared within a function, apparently PHP tries to redeclare it as soon as you call the parent function the 2nd time, which doesn't make any sense to me.
PHP Fatal error: Cannot redeclare current_tab() (previously declared in .../wp-content/plugins/dtabs/dtabs.php:1638) in .../wp-content/plugins/dtabs/dtabs.php on line 1638
The code for that revision is at http://plugins.svn.wordpress.org/!svn/bc/208481/dtabs/trunk/dtabs.php
What I'm trying to figure out is whether there is a way to tell PHP that yeah... it has an internal function, which is a perfectly valid PHP paradigm as far as I know, so don't redeclare it and fail.
As for the situation at hand, I have removed current_tab() as it doesn't appear to be used.
You can use function_exists() to test if a function with that name has already been defined. If you make the definition conditional ( if(something) { function foo() {...} } ) php will "evaluate" the definition only when the condition is met.
function foo() {
if ( !function_exists('bar') ) {
function bar() {
echo 'bar ';
}
}
bar();
}
foo();
foo();
see also: http://docs.php.net/functions.user-defined
(But I'd try to avoid such things all together)
You can wrap your function declaration in an if statement. Use function_exists() to see if the function has been previously declared or not.
if(!function_exists('current_tab')) {
function current_tab() {
myMagicCode();
}
}
You can try this:
if (!function_exists('my_function')) {
function my_function() {
}
}
function_exists() - Return TRUE if the given function has been defined
I get an error that says
Fatal error: Call to undefined method stdClass::mysql_con() in ........../.../includes/script/import.php on line 68.
Line 68 corresponds to:
if(!$ip2c->mysql_con())
I do have a require_once() statement at the beginning of my script
What could be the problem here?
Thanks
Dusoft says it could mean:
$ip2c object does not exist,
Which is not correct because you would get a different error "Fatal error: Call to a member function mysql_con() on a non-object"
He also says it could mean:
mysql_con function is not part of the class you are trying to call
Which is true but not so helpful cos its very difficult to add methods to stdClass.
Additionally it could be to do with serialisation quote:
This error is normally thrown when a class instance has been serialised to disk, then re-read/deserialised in another request but the class definition has not been loaded yet, so PHP creates it as an "stdClass" (standard class.)
Or most likely, I think:
the $ip2c variable was not an object and then php silently cast it to become stdClass somewhere in the code above.
This could happen if you directly assign a property on it.
Like:
$ip2c = null;
//php casts $ip2c to 'stdClass'
$ip2c->foo = bah;
//Fatal error: Call to undefined method stdClass::mysql_con() in...
$ip2c->mysql_con();
See a better example here.
it means that either $ip2c object does not exist or mysql_con function is not part of the class you are trying to call.
I think this happen because "extension=php_mysql.dll" extension isn't loaded in php.ini.
Take a look with
phpinfo();
It could be incorrect code. I once managed to get that error when I had this line of code:
if ($myObj->property_exists('min')){
// do something
}
Which resulted in error line like this:
PHP Fatal error: Call to undefined method stdClass::property_exists() in myFile.php on line ###
I later fixed the line to:
if (property_exists($myObj, 'min')) {
// do something
}
So check for that possibility as well.
Most likely the object does not exist. Please show us the code of how you created it. If you are using it within another class (maybe creating it in the __construct function for example), using:
$ip2c = new Class;
Won't cut it. Instead do:
$this->ip2c = new Class;
and then
$this->ip2c->mysql_con();