jsdalton
jsdalton

Reputation: 6835

Can I get PHP to throw an error when I try to define a function that has already been defined?

I run into this problem periodically, and I'm trying to figure out if it's a configuration issue or a peculiarity with PHP.

Basically, there will be a function foo() defined somewhere in my code, and then elsewhere I will accidentally define a function again with the same name foo(). I would expect an error of some kind to be thrown, e.g. "Fatal Error: Function already defined line 234, bar.php blah blah".

But I get nothing. I just get a blank screen. Debugging can take an eternity as I try to pinpoint exactly where the function is being accidentally redefined, without help from an error message.

My config settings for reporting errors are set to E_ALL, and I see all other kinds of errors without a hitch.

Any insights? Is there anything I can do to cause PHP to report these errors (so that I can either rename the offending function or wrap it in an if function_exists() clause)?

Edit: To be clear, I understand the many strategies to avoid namespace collisions in PHP. I'm talking about instances where for whatever reason I accidentally name a function a name that already exists during development (or for example I import a file where a function has already been defined). I would expect an error message when this occurs, which would help me debug, but I do not get one.

Upvotes: 2

Views: 929

Answers (5)

Pekka
Pekka

Reputation: 449613

You can wrap function definitions in a conditional function_exists():

if (!function_exists("file_get_contents"))
 {
   function file_get_contents(....)
     ....
 }

works for me.

You could wrap a function_exists check around each function and have it throw an error message if it already exists. The better way however would be finding out why fatal errors don't appear on screen. Can you check phpinfo() for the error_reporting and related settings?

This is what you should be getting when trying to redefine a function:

Fatal error: Cannot redeclare file_get_contents() in D:\xyz\htdocs\test.php on line 5

From my phpinfo():

display_errors On

error_reporting 22519 (equals... well, I don't know but it shows errors :)

In some hosting environments when error reporting was turned off completely, I have found defining a custom error handler very helpful: set_error_handler()

Upvotes: 4

TravisO
TravisO

Reputation: 9550

It seems you're not organizing your code properly, try using a common library that you include in your files, and define functions there. Perhaps you just need to start uses classes, then you won't have collisions when you have two functions with the same name.

Upvotes: 0

AlishahNovin
AlishahNovin

Reputation: 1932

Some suggestions to make your life easier:

I typically will create a generic functions file, where I store all my global functions that I'll be using throughout my app.

I'll also try and remain as object-oriented as possible. PHP will give errors if you're creating an object that already exists, and by encapsulating your functions into logical objects you'll have an easier time maintaining it.

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382806

As an alternative, you can actually check whether a function is already there by using function_exists.

if (function_exists('foo'))
{
  echo 'foo function exists !!';
}
else
{
  echo 'foo function does not exists !!';
}

Upvotes: 0

Ben Everard
Ben Everard

Reputation: 13804

Are you hosting the script on your local server? I know a UK based hosting company that prevent any PHP errors from being returned at all, even if you've set E_ALL.

If this is the case consider testing the app locally with error reporting turned on.

Upvotes: 1

Related Questions