email issue - PHP
This is a discussion on email issue - PHP ; 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
...
-
Re: [PHP] PHP and #if
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
-
Re: [PHP] PHP and #if
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
-
Re: [PHP] PHP and #if
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();
?>
-
Re: [PHP] PHP and #if
OK, here's how it goes:
<?php
if(!defined('_THIS_PHP_FILE_PHP')) {
define('_THIS_PHP_FILE_PHP', true);
// define your classes and functions here
} // endif _THIS_PHP_FILE_PHP
?>
-
Re: [PHP] PHP and #if
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/
-
RE: [PHP] PHP and #if
> -----Original Message-----
> From: Eric Gorr [mailto:mailist@ericgorr.net]
> Sent: Friday, March 14, 2008 3:22 PM
> To: PHP General
> Subject: Re: [PHP] PHP and #if
>
>
> 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
>
>
>
Mmmm... why would you want to use a different class definition on some
conditions? Yes, there might be reasons, but it's usually just a matter of
realizing that you can use inheritance, containment or some design patterns
(say, the Adapter pattern).
There are other ways to solve the problem which are not yet available in PHP.
Some of them are being discussed nowadays
(http://wiki.php.net/rfc/nonbreakabletraits).
Now, if you want something weird... I believe this would work:
<?php
ob_start();
?>
class TestClass
{
<?php
if ( rand(0,1) == 0 )
{
?>
function arse()
{
echo "arse 1\n";
}
<?php
}
else
{
?>
function arse()
{
echo "arse 2\n";
}
<?php
}
?>
}
<?php
// Fetch class definition in the output buffer;
$classDef = ob_get_clean();
// Define class
eval($classDef);
$myVar = new TestClass;
$myVar->arse();
?>
I didn't test this, but it should work I think... I remember nuSoap doing
something similar for soap proxys (though not using output buffering). There are
alternatives that are way better (and smarter about performance) than this,
but... you see... everything can be done in PHP.
If you look for "build-like" tools, that is, generate code at "deployment-time",
you may try phing http://phing.info.
Regards,
Rob
Andrés Robinet | Lead Developer | BESTPLACE CORPORATION
5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308 |
TEL 954-607-4296 | FAX 954-337-2695 |
Email: info@bestplace.net | MSN Chat: best@bestplace.net | SKYPE: bestplace |
Web: bestplace.biz | Web: seo-diy.com