Go around cannot redeclare error in PHP - php

I am trying to integrate WordPress and MyBB forums. Specifically, I just want to add WordPress's navbar (the new menu) to my MyBB website. Following the CODEX example, I've added the following to my header.php of my MyBB installation:
require('/home/linuxdis/public_html/wp-blog-header.php');
However, I get 500 error when navigating to the forum. Examining error_log revealed this:
PHP Fatal error: Cannot redeclare is_super_admin() (previously declared in /home/linuxdis/public_html/forum/inc/functions.php:5484) in /home/linuxdis/public_html/wp-includes/capabilities.php on line 1213
Bummer, the functions are named the same. Other than renaming one of the functions and probably breaking absolutely everything, is there a way to go around this? :/

User require_once() instead of require() and it will make sure it will only include that file once per page.
Although not ideal in this case, you could wrap each function in a function_exists() check:
if(!function_exists('myfunc') {
function myfunc() {....}
}
This might be your only option if there are direct clashes with WP/MyBB, you're stuck if they both need is_super_admin() though.

Related

How is it possible for the application to throw a redeclaration error on deleted and unexisting function?

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

What is the correct way to include PHP functions in Joomla for use in articles?

I am using Joomla 2.5 and DirectPHP. I can run PHP code in an article just fine. I would like to include a library of PHP functions to be available for use in all articles. What is the best way to include the function library?
I tried including (with require) from the template's index.php and even putting the functions directly in index.php, as suggested here. None of the functions are defined in the article. A test with variables and global variables also finds them undefined in the article.
I then created a custom HTML module with the PHP functions that I then used in my template, but they are still undefined in the article. Oddly enough, if I try and declare the function again in the article I get "Cannot redeclare...". How can the function be both undefined, not re-defineable?
I also tried using a namespace to define and use my functions. Same result.
Code in my custom html module:
<?php
namespace c6;
function testit5()
{
echo "hello world 5";
}
?>
Code in my article:
<?php
namespace c6;
testit5();
?>
Result:
Fatal error: Call to undefined function c6\testit5() in /home/testsite/www/www/c6test/plugins/content/DirectPHP/DirectPHP.php(58) : eval()'d code on line 5
go to,
Login to admin -> Extensions Menu -> Module Manager -> Create New Module -> Choose Custom HTML
and create you own module and add php functions.
I figured it out! My problem was the order Joomla processes things. Apparently the article is processed first, then the template. So my code using the function executed before the module that defined the function was executed. (When I got the "cannot redeclare" error, it was coming from the template, not the article.) I moved my loadposition from the template to the article and it works.

Can't load main website or the Magento /admin page. Just loads as a blank page! Error log provided

Our site has been down since I tried doing a backup of our Magento site. This was the last thing I did that could have triggered something. I had made other changes throughout the day, but only playing with some extensions, and this was a couple hours prior to the error. Below is the last entry to the error log:
[16-Jun-2015 18:42:56 UTC] PHP Fatal error: Call to a member function
setLayer() on a non-object in
**public_html/app/code/core/Mage/Catalog/Block/Layer/View.php on line 134
Got into the line its calling to but have no idea what to do with it! Not even sure if this is the direct cause of this issue. I feel something should still load.
Could it be something simple I'm missing like a Maintenance flag?
Any other reasons why the site could be behaving like this? Quite frustrating that I cant even reach the admin page. All our other subdomain site work too.
It would appear to me that somewhere in your coding of some extensions you broke something. This error happens when you have what you think is an object but is actually not and you call a method on it.
for instance if you have something like the following
<?php
$obj = function_to_get_object('name');
$obj->getName();
if the function_to_get_object does not return an object but rather returns NULL or false then calling the method getName will fail because the variable is not an object rather NULL or FALSE.
I could not see this happening with a Magento install (without custom code) unless maybe you deleted a file or something and the base install cannot find the file to include or instantiate the object.

PHP Fatal-Error: Call to a member function append_output()

I use codeigniter in my blog and since a while I get this error
PHP Fatal error: Call to a member function append_output() on a non-object in /var/www/site/blog/system/core/Loader.php on line 862
I don't know what change caused this and why it appears. The site gets rendered and send to browser completely, from views header.php, index.php to footer.php everything is there and after that this error appears. Search with google showed another site, that has this error at the very bottom of their site...
I now supressed the error with error_reporting(0) as the whole site works fine, but that's not a solution I want to stay with.
It happens on all pages, I have one Controler (blog.php) and several methods like index(), article(), archive() in it. The methods do what they are supposed to do, but when CI finished rendering the page, the error appears, with all controler methods.
What can I do to trace where this problem appears?
https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Loader.php#L938
If the error is occurring on the value returned from get_instance, here will be your problem. Although you may have to look at the version you are using to get the right line number.
Additionally:
https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Controller.php#L75
This appears to be the singleton class that function leads to, it is returning self::$instance which is created in the constructor.
To me this means the CI_Controller singleton has not been instantiated at the time that error has occurred.
Hope that helps you debug your problem.
I had the same problem. I'd overwritten the output class ($this->output) in my controller.

ModX Evo: PHP error in document.parser.class.inc.php

So my Evo site stopped working the other day - just got a 500 error. I got my host to check the logs and found this:
[error] PHP Fatal error: Cannot redeclare insert_metka() (previously declared in
/home/mysite/public_html/manager/includes/document.parser.class.inc.php(790) : eval()'d code:2)
in /home/mysite/public_html/manager/includes/document.parser.class.inc.php(790) : eval()'d code on line 12
I have tired commenting out the offending line and removing the entire file to no avail. Does anyone know what this means and how to fix it?
EDIT: the code at line 790:
eval ($pluginCode);
Looks like a bad plugin has broken your site. Disable all your plugins and reinstate them one at a time until it breaks again, then you know which one is the culprit.
Once you've done that, post the plugin code here and we can help you debug it further. You shouldn't ever need to modify the MODX source code.
The problem is likely to be solved by wrapping the insert_metka() declaration like this:
if(!function_exists('insert_metka')) {
function insert_metka() {
// function code
}
}
http://wiki.modxcms.com/index.php/Creating_Snippets#Wrap_functions_inside_.21function_exists_conditional_if_the_resource_is_needed_to_run_more_than_once_on_a_page
That appears like a very simple problem. You are declaring insert_metka() two times. Remove it from one of the mentioned files. Once it is declared in saved file and apparently second time you are trying to declare it with the help of eval()
Cannot redeclare insert_metka() says that you are declaring a function twice (may be with your evals).
You should check on your included files.
To be sure to not include more than one time, you can use include_once or requiere_once instead of include and requiere
Accoring to document.parser.class.inc.php this line i guess you are using OOP.
So what you can do is create instantiation class for above class and then overwrite it.
There are other things too. you might declaring same function inside of same class.

Categories