note 73120 modified in function.function-exists by danbrown
| From: | danbrown@php.net | Date: | Sat, 19 Jul 2008 20:42:18 +0000 |
| Subject: | note 73120 modified in function.function-exists by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141725@lists.php.net to get a copy of this message | ||
Note that function_exists will return TRUE in the following situation, presumably because the
function "testfunc" was defined when the script was PARSED/ITERPRETED, before the
function_exists call was made at RUNTIME:
<?php
if (function_exists('testfunc')) return;
function testfunc() { }
?>
So, this construction is not useful for preventing testfunc from being multiply defined if the
script is muliply included or required.
However, the following construction DOES work to prevent multiple defines of testfunc:
<?php
if (!function_exists('testfunc')) {
function testfunc() { }
}
?>
CONTRAST this with similar uses of defined() which is completely runtime evaluated. These both
work:
<?php
if (defined('testfunc_defined')) return;
define('testfunc_defined', 1);
function testfunc() { }
?>
AND...
<?php
if (!defined('testfunc_defined')) {
define('testfunc_defined', 1);
function testfunc() { }
}
--was--
Note that function_exists will return TRUE in the following situation, presumably because the
function "testfunc" was defined when the script was PARSED/ITERPRETED, before the
function_exists call was made at RUNTIME:
<?
if (function_exists('testfunc')) return;
function testfunc() { }
?>
So, this construction is not useful for preventing testfunc from being multiply defined if the
script is muliply included or required.
However, the following construction DOES work to prevent multiple defines of testfunc:
<?
if (!function_exists('testfunc')) {
function testfunc() { }
}
?>
CONTRAST this with similar uses of defined() which is completely runtime evaluated. These both
work:
<?
if (defined('testfunc_defined')) return;
define('testfunc_defined', 1);
function testfunc() { }
?>
AND...
<?
if (!defined('testfunc_defined')) {
define('testfunc_defined', 1);
function testfunc() { }
}
http://php.net/manual/en/function.function-exists.php