Page 1 of 1

Redeclare functions

Posted: Sat Nov 14, 2009 7:47 am
by Zepx
Script URL:
Version of script: 1.7
Hosting company: none
URL of phpinfo.php:
URL of session_test.php:
What terms did you try when SEARCHING for a solution: redeclare functons

Write your message below:

Hi,

I'm basically integrating linkman 1.7 into my wordpress theme. However, I don't have much problem with that. What I would like to highlight is regarding settings.php

inside settings.php, I noticed that there are 2 functions. If I've 2 scripts that decided to require 'settings.php'; I would obtain an error because I've declared the functions twice. Meanwhile, if I had it require_once 'settings.php'; it would be possible, but somehow, $settings was not set with values. I don't know why though.

so I was wondering, if those two functions should belong inside settings.php? Or, should it be included as another file in addlink.php and other files that requires this function.

Or, is there another solution to this problem?

Posted: Sat Nov 14, 2009 11:02 am
by Klemen
The solution is to rename "setting.php" to "setting2.php" and all instances of "setting.php" within the script code (in all files) to "setting2.php".

If you do this for LinkMan or the other script doesn't matter, whichever is less work.

Posted: Sat Nov 14, 2009 12:56 pm
by Zepx
Oh well, I guess there is no good workround other than this. But how does renaming helps?

I've added an extra function_exists() to check if both functions were declared to avoid function redeclared error.

Posted: Sat Nov 14, 2009 5:11 pm
by Klemen
Sorry, I was thinking about something and writing about something else. The function name needs to be renamed.

Don't just check if the function exists, because the LinkMan function, that in your case has the same name as some other function, most likely doesn't give the same results.

Posted: Sun Nov 15, 2009 12:26 am
by Zepx
actually, I think you have got the wrong idea. On runtime, I don't think it's possible to rename the functions.

I had no choice but use a function_exists() check. The reason was simply, because if I use require_once, I would obtain errors, this is due to $settings not loaded. This happens when I have 2 separate scripts running at the same time, and uses require 'settings.php';.

The 2 separate scripts would call settings.php twice, resulting a redeclare of functions. Unless, there is another workaround to declare $settings. Because, from what I see, $settings seems to cleared up after the 1st script uses it, making $settings to be nulled.

Anyway, I found that it has to be a problem with the scope of the $settings variable.

My suggestion was, in future, couldn't those functions declared in settings.php be included as another function file? That would simply allow integration easily.

Thanks again.

Posted: Sun Nov 15, 2009 9:34 am
by Klemen
Wait, are you using two different scripts (each with it's own settings.php file) or are you using two copies of LinkMan? Because it makes all the difference. The solution with function_exist would only work properly in the latter case, my solution (renaming function) assumed that you have two completely different scripts each using it's own settings.php file.

Posted: Sun Nov 15, 2009 11:43 am
by Zepx
Hi,

Take it this way, since links.php displays generates the form, I took out the display and left the generate part in links.php. The second script I was talking about was display.php.

I've that so that, when I use access my home page, I only need display.php. When I access the link-exchange page, I need both display.php and links.php.

display.php requires settings.php, so does links.php. Therefore, it is necessary for me to only load settings.php once. If I load settings.php twice, there will be a redeclare error.

In short, I think I'm using two different scripts (one generate and parse the form and the other displays), both loads the very same settings.php. Focing my script to load another settings.php would mean that the script is no longer dynamic.

------

Actually, I found out my problem. require_once works just fine, however, the wordpress structure is in such a way:

Inside index.php

Code: Select all

call_function1();
call_function2();
Inside call_function1()

Code: Select all

require 'links.php';
Inside call_function2()

Code: Select all

require 'display.php';
Inside links.php

Code: Select all

require_once 'settings.php';
Inside display.php

Code: Select all

require_once 'settings.php';
If you get the idea, I think $settings was loaded as a local scope in call_function1() and call_function2(). So, $settings was not possible to be accessed in call_function2(). Instead, since it doesn't matter where you declare a function as it is accessible anywhere in the script, the script will declare the function twice.

Posted: Sun Nov 15, 2009 3:28 pm
by Klemen
I think I see what your problem was - you should call the settings file ONCE within the entire file (at the beginning) and then declare $settings as global within functions.

require settings.php

function one()
{
global $settings;
/* the rest of the code... */
}

function two()
{
global $settings;
/* the rest of the code... */
}

If you just include files within functions without declaring globals the $settings of course won't be available outside that function.

Posted: Mon Nov 16, 2009 5:36 am
by Zepx
Thanks I actually figured that out after looking further. However, I still think I'll be using function_exists since if I were to fixed it in such a manner, it will not be dynamic. I would edit the template of my wordpress at last resort, but function_exists solved everything :).