Re: PHP and #if

From: Date: Fri, 14 Mar 2008 19:51:11 +0000
Subject: Re: PHP and #if
References: 1 2 3 4 5 6 7 8 9 10  Groups: php.general 
Request: Send a blank email to php-general+get-271615@lists.php.net to get a copy of this message
On 14 Mar 2008, at 19:21, Eric Gorr wrote:
On Mar 14, 2008, at 3:15 PM, Eric Gorr wrote:
On Mar 14, 2008, at 3:10 PM, Stut wrote:
On 14 Mar 2008, at 19:03, Eric Gorr wrote:
Unfortunately, such things cannot be used to wrap functions.
Erm, yes they can. Try it. <?php if (rand(0,1) == 0) {
     function arse()
     {
         echo "arse 1\n";
     }
} else {
     function arse()
     {
         echo "arse 2\n";
     }
} arse(); ?>
Gives: Parse error: syntax error, unexpected T_STRING in /Users/Eric/Sites/ifWrapping.php on line 3
Oh, sorry, apparently there are some invisible characters in the text you pasted which I had to zap first. Yes, this does work as expected. However, try wrapping the arse function in a class. <?php class TestClass { if ( rand(0,1) == 0 ) { function arse() { echo "arse 1\n"; } } else { function arse() { echo "arse 2\n"; } } } $myVar = new TestClass; $myVar->arse(); ?>
In my experience there are very few valid reasons for conditionally defining functions, and even fewer for conditionally defining methods in a class. Maybe if you explain what you're trying to achieve we can help you find a better way. -Stut -- http://stut.net/

« previous php.general (#271615) next »