Page 1 of 1

How to redeclare a function

Posted: Wed Feb 14, 2007 10:06 am
by guarriman
Hi.

I want to create a program which modifies 'foo()' function if a situation happens.

I don't want to insert an 'IF' within the function, since I want not to modify the original 'functions.php' file. But if I redeclare 'foo()' I get this error message:
-------
Cannot redeclare foo() (previously declared in /home/test/functions.php)
-------

How to redeclare the function? Thank you very much.

Posted: Wed Feb 14, 2007 10:17 am
by Jenk
In short - you can't.

Also, fyi: This forum has a PHP Code sub-forum, which is surprsingly for PHP Code problems, queries and discussions! Fancy that!

Posted: Wed Feb 14, 2007 10:18 am
by Chris Corbyn
You can't*. Rethink your design. If foo() needs to do different things, make it call two different functions foo1() and foo2().

* Maybe runkit would help -- but it would be hacked up job of the century.

Posted: Wed Feb 14, 2007 10:25 am
by zyklone
if you want to redeclare you function use OO.

Code: Select all

class myfoo
{
   function foo()
  {
  }
}

class myfoo2 extends myfoo
{
   function foo()
   {
   }
}

Code: Select all

myfoo::foo();
myfoo2::foo();

Posted: Wed Feb 14, 2007 10:28 am
by Chris Corbyn
Nothing OO about that ;) Sorry, I had to be a pedant for a second :lol:

Posted: Wed Feb 14, 2007 11:17 am
by RobertGonzalez
Moved to PHP - Code.