note 75080 deleted from function.define by tularis
| From: | tularis@php.net | Date: | Sun, 03 Jun 2007 09:36:04 +0000 |
| Subject: | note 75080 deleted from function.define by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127138@lists.php.net to get a copy of this message | ||
Note Submitter: Wolverine
----
Take a look at the following code:
<?
define('SOME_FUNC', myfunc());
define('MAXVAL', 10);
class Foo
{
private $test;
function set()
{
$this->test( myfunc );
}
}
function myfunc()
{
return 'some text';
}
function otherfunc()
{
echo rand(0, MAXVAL);
}
$c=new Foo();
$c->set();
otherfunc();
?>
This will break constants and cause weird, unpredictable behaviour. Foo::$test variable will be set
properly with 'some text', but if you try to use numeric constants like MAXVAL outside the
class, in this case in otherfunc() PHP won't find this constant. If you use
defined('MAXVAL') in otherfunc() it will tell you it's defined, but it won't
work if you use this constant within the function.
So be warned, and avoid using functions as constant parameters, because it's probably PHP bug
and can cause serious disruption in your program. It's interesting to notice that string type
constants won't have this problem, it's related only to constants with numeric values.