note 75080 added to function.define
| From: | Wolverine at osu1 dot php dot net | Date: | Fri, 11 May 2007 10:47:03 +0000 |
| Subject: | note 75080 added to function.define | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126406@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 195.136.184.34
Probable Submitter: 85.219.142.101
----
Manual Page -- http://www.php.net/manual/en/function.define.php
Edit -- https://master.php.net/note/edit/75080
Del: integrated -- https://master.php.net/note/delete/75080/integrated
Del: useless -- https://master.php.net/note/delete/75080/useless
Del: bad code -- https://master.php.net/note/delete/75080/bad+code
Del: spam -- https://master.php.net/note/delete/75080/spam
Del: non-english -- https://master.php.net/note/delete/75080/non-english
Del: in docs -- https://master.php.net/note/delete/75080/in+docs
Del: other reasons-- https://master.php.net/note/delete/75080
Reject -- https://master.php.net/note/reject/75080
Search -- https://master.php.net/manage/user-notes.php