Re: PHP and #if

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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();
> ?>
> 
> 
> That fails with:
> 
> 
> Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in
> /Users/Eric/Sites/ifWrapping.php on line 4
> 
> 
Never tried it in a class.  There is probably a way to hack and get it
to work.  However, unless you're doing something so much more
sophisticated than most people, what's wrong with this?

<?php
class TestClass
{
    function arse()
    {
        if ( rand(0,1) == 0 )
        {
            echo "arse 1\n";
        }
        else
        {
            echo "arse 2\n";
        }
    }
}

$myVar = new TestClass;
$myVar->arse();
?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux