Jump to content

How to use function_exists() without warnings/errors


bigglescdh

Recommended Posts

Hi,

Apologies if this is a dumb question, I'm new to the forum.

I'm using Zend Studio IDE, and trying to tidy up some code. Zend IDE gives warnings "Cannot re-declare function" with the following code (this example from wordpress) which I have seen used quite a lot.

As far as I can see, because of the If/else  the snippet will NOT re-declare ? - what is the correct way to code this snippet to avoid warnings? (is that possible??) or is it just code that Zend "doesn't like" :)

 

if ( !function_exists('wp_nonce_field') ) {

function akismet_nonce_field($action = -1) { return; }

$akismet_nonce = -1;

} else {

function akismet_nonce_field($action = -1) { return wp_nonce_field($action); }

$akismet_nonce = 'akismet-update-key';

}

 

Many thanks

Biggles

This declares function akismet_nonce_field() so you should also check if it exists or not before.

Indeed it does, but the actual warning appears only on line 5 and says Cannot re-declare function 'askimet-nonce-field'

So Zend seems to ignore the If/else construct

Try like this:

 

if ( !function_exists('akismet_nonce_field') ) {
  if ( !function_exists('wp_nonce_field') ) {
     function akismet_nonce_field($action = -1) { return; }
     $akismet_nonce = -1;
  } else {
     function akismet_nonce_field($action = -1) { return wp_nonce_field($action); }
     $akismet_nonce = 'akismet-update-key';
  }
}

[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.