Welcome Guest, Not a member yet? Register   Sign In
general answers to 'cannot redeclare' problem in integrations
#1

[eluser]Grahack[/eluser]
Hi everyone,
Suppose you are trying to integrate two wonderful php apps together. How do you proceed when you find that they use the same name for two functions and you get the 'cannot redeclare' thing? Do you have a strategy for choosing one or the other to rename? Are there other solutions?
Thanks.
#2

[eluser]xwero[/eluser]
I think pseudo namespacing comes in handy. The keys in the language files, the keys in the session userdata, the names of the helper functions can all be prefixed to avoid problems before they exist.

You could do a function_exists check but if they are custom functions the chances are the the function doesn't do what you want it to do. You could try something like
Code:
if(function_exists('add'))
{
   $newname = create_function('$a,$b', 'return $a+$b;');
   $newname($this->input->post('field1'),$this->input->post('field2'));
}
else
{
   function add($a,$b)
   {
       return $a+$b;
   }
}
#3

[eluser]ejangi[/eluser]
How good life will be when PHP6 arrives with it's namespacing... *sigh* those'll be the days.
#4

[eluser]xwero[/eluser]
I believe php5.3 will already have namespacing
#5

[eluser]Grahack[/eluser]
Ok, thanks xwero, but maybe I was not clear: suppose I'm not responsible for any of the source codes involved.
Like integrating an existing wiki and an existing forum in the same page (well, quite silly, but who knows!). Each soft needs its function.

Moreover, I didn't know about this create_function() thing. Thanks for the tips but I don't understand the purpose here. $newname can't be accessed from somewhere else can it?
#6

[eluser]ejangi[/eluser]
Yes, if $newname is defined in the global namespace, you can then call $newname() from anywhere in your code. create_function() is VERY slow though, so if you help it, try not to use it, but if you have to it's handy.
#7

[eluser]xwero[/eluser]
I don't think there is a way where you don't have to hack the existing code and be able to run applications that have functions with the same name.
#8

[eluser]Grahack[/eluser]
Ok, that's what I thought. Then how do you decide which one to hack?

Thanks guys, I'm not a very informated php user, so your namespaces infos are well received.
#9

[eluser]tonanbarbarian[/eluser]
I guess this all depends on how much the 2 apps need to integrate.
Is it possible to have them each run seperately

for example if app X and app Y are so different that when viewing X nothing of Y has to be loaded then you do not have a problem
If however they do have to run together some how the only option i can think of is to run one remotely

if you curl, or fsockopen to one of them it will run in its own process
however this can not always work

or maybe one can be run via an API rather than directly

if neither of these options are possible then you have to modify the code in one of them to resolve any name conflicts
#10

[eluser]xwero[/eluser]
I would go for the application with the smallest codebase, less code to alter so it's also more app upgradable friendly.
If they have the same size code base i would choose the one i think i would need to join with other applications more often than the other. In your example of the wiki-forum join i would hack the wiki.




Theme © iAndrew 2016 - Forum software by © MyBB