Forum Moderators: coopster

Message Too Old, No Replies

function overwritting

how do i unset or over write a function?

         

benj0323

6:33 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



I ahve an instance where I need to over write a function.

For example:

function someFunc() {
return true;
}

function someFunc() {
return false;
}

If you do that in php 5 it gives you an error. Any way I can accomplish that?

Thanks a lot for your help.

vincevincevince

7:54 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1/ put your functions into a class so you don't have to have them in the public scope

or:
2/ maybe use a static variable (or a global variable) and switch between behaviours:


function foo()
{
global $whichfoo;
switch ($whichfoo)
{
case "1" : {return true;}
case "2" : {return false;}
}
}

You can then, instead of trying to replace foo(), just change $whichfoo...

not: function foo() { return true;}
but: $whichfoo = 1;

not: function foo() { return false;}
but: $whichfoo = 2;

coopster

8:41 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Another option may be to check if the function_exists() [php.net] and if so, create_function() [php.net] with a new name.

mincklerstraat

7:13 pm on Oct 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yes, that'd be a great way to do it.

Reason here being you can't overwrite functions in PHP (neither 4 nor 5).

You can even just use standard function declaration notation inside an if statement -


if(!function_exists('somefunction')){
function somefunction($somevar){
/* do some stuff */
}

}


Or you can of course use other if conditions as well.