How to redeclare a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
guarriman
Forum Commoner
Posts: 44
Joined: Thu Nov 03, 2005 4:11 am

How to redeclare a function

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
zyklone
Forum Commoner
Posts: 29
Joined: Tue Nov 28, 2006 10:25 pm

Post 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();
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Nothing OO about that ;) Sorry, I had to be a pedant for a second :lol:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Moved to PHP - Code.
Post Reply