Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14679 > unrolled thread

Consistency with object to boolean conversions

Started byGregor Kofler <usenet@gregorkofler.com>
First post2014-12-03 15:56 +0100
Last post2014-12-03 20:58 +0100
Articles 12 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Consistency with object to boolean conversions Gregor Kofler <usenet@gregorkofler.com> - 2014-12-03 15:56 +0100
    Re: Consistency with object to boolean conversions "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-03 16:19 +0100
      Re: Consistency with object to boolean conversions Gregor Kofler <usenet@gregorkofler.com> - 2014-12-03 16:39 +0100
        Re: Consistency with object to boolean conversions Matthew Carter <m@ahungry.com> - 2014-12-03 11:42 -0500
          Re: Consistency with object to boolean conversions Gregor Kofler <usenet@gregorkofler.com> - 2014-12-03 18:06 +0100
            Re: Consistency with object to boolean conversions Matthew Carter <m@ahungry.com> - 2014-12-03 13:13 -0500
    Re: Consistency with object to boolean conversions Denis McMahon <denismfmcmahon@gmail.com> - 2014-12-03 16:46 +0000
      Re: Consistency with object to boolean conversions Gregor Kofler <usenet@gregorkofler.com> - 2014-12-03 18:09 +0100
        Re: Consistency with object to boolean conversions "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-03 18:22 +0100
          Re: Consistency with object to boolean conversions Gregor Kofler <usenet@gregorkofler.com> - 2014-12-03 19:12 +0100
            Re: Consistency with object to boolean conversions "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-03 19:30 +0100
              Re: Consistency with object to boolean conversions Gregor Kofler <usenet@gregorkofler.com> - 2014-12-03 20:58 +0100

#14679 — Consistency with object to boolean conversions

FromGregor Kofler <usenet@gregorkofler.com>
Date2014-12-03 15:56 +0100
SubjectConsistency with object to boolean conversions
Message-ID<m5n8a7$gra$1@dont-email.me>
The following example should suffice to describe my problem:

50 if(!$file) {
51 }
52 else {
53    echo '$file is an object.';
54    var_dump(!$file);
55 }

results in

"$file is an object.

Catchable Fatal Error: Object of class 
Symfony\Component\HttpFoundation\File\UploadedFile could not be 
converted to boolean in 
/var/www/strbase_client/src/Strbase/ClientBundle/Controller/BatchController.php 
line 54"

Why does the object to boolean conversion in the if condition in line 50 
*not* cause an error?

Gregor

[toc] | [next] | [standalone]


#14680

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2014-12-03 16:19 +0100
Message-ID<m5n9l9$djm$1@solani.org>
In reply to#14679
Gregor Kofler wrote:

> The following example should suffice to describe my problem:
> 
> 50 if(!$file) {
> 51 }
> 52 else {
> 53    echo '$file is an object.';
> 54    var_dump(!$file);
> 55 }
> 
> results in
> 
> "$file is an object.
> 
> Catchable Fatal Error: Object of class
> Symfony\Component\HttpFoundation\File\UploadedFile could not be
> converted to boolean in
> /var/www/strbase_client/src/Strbase/ClientBundle/Controller/BatchController.php
> line 54"
> 
> Why does the object to boolean conversion in the if condition in line 50
> *not* cause an error?

What gets printed when you change line 51 to:

  var_dump($file);}

I guess that answers your question.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#14681

FromGregor Kofler <usenet@gregorkofler.com>
Date2014-12-03 16:39 +0100
Message-ID<m5narg$rvn$1@dont-email.me>
In reply to#14680
Am 2014-12-03 um 16:19 schrieb Christoph M. Becker:
> Gregor Kofler wrote:
>
>> The following example should suffice to describe my problem:
>>
>> 50 if(!$file) {
>> 51 }
>> 52 else {
>> 53    echo '$file is an object.';
>> 54    var_dump(!$file);
>> 55 }
>>
>> results in
>>
>> "$file is an object.
>>
>> Catchable Fatal Error: Object of class
>> Symfony\Component\HttpFoundation\File\UploadedFile could not be
>> converted to boolean in
>> /var/www/strbase_client/src/Strbase/ClientBundle/Controller/BatchController.php
>> line 54"
>>
>> Why does the object to boolean conversion in the if condition in line 50
>> *not* cause an error?
>
> What gets printed when you change line 51 to:
>
>    var_dump($file);}
>
> I guess that answers your question.


Erm... no. Line 51 is not reached - hence I get precisely the same 
output as above.
$file exists and is a UploadedFile instance.

[toc] | [prev] | [next] | [standalone]


#14682

FromMatthew Carter <m@ahungry.com>
Date2014-12-03 11:42 -0500
Message-ID<87d280ofau.fsf@ahungry.com>
In reply to#14681
Gregor Kofler <usenet@gregorkofler.com> writes:

> Am 2014-12-03 um 16:19 schrieb Christoph M. Becker:
>> Gregor Kofler wrote:
>>
>>> The following example should suffice to describe my problem:
>>>
>>> 50 if(!$file) {
>>> 51 }
>>> 52 else {
>>> 53    echo '$file is an object.';
>>> 54    var_dump(!$file);
>>> 55 }
>>>
>>> results in
>>>
>>> "$file is an object.
>>>
>>> Catchable Fatal Error: Object of class
>>> Symfony\Component\HttpFoundation\File\UploadedFile could not be
>>> converted to boolean in
>>> /var/www/strbase_client/src/Strbase/ClientBundle/Controller/BatchController.php
>>> line 54"
>>>
>>> Why does the object to boolean conversion in the if condition in line 50
>>> *not* cause an error?
>>
>> What gets printed when you change line 51 to:
>>
>>    var_dump($file);}
>>
>> I guess that answers your question.
>
>
> Erm... no. Line 51 is not reached - hence I get precisely the same
> output as above.
> $file exists and is a UploadedFile instance.
>
>

This works for me:

class a {}

$b = new a ();

if (!$b)
  {
  }
else
  {
    echo '$a is an object!';
    var_dump (!$b);
  }

Maybe its an issue related to your specific class?


-- 
Matthew Carter (m@ahungry.com)
http://ahungry.com

[toc] | [prev] | [next] | [standalone]


#14685

FromGregor Kofler <usenet@gregorkofler.com>
Date2014-12-03 18:06 +0100
Message-ID<m5nfuo$jbu$1@dont-email.me>
In reply to#14682
Am 2014-12-03 um 17:42 schrieb Matthew Carter:
> Gregor Kofler <usenet@gregorkofler.com> writes:
>
>> Am 2014-12-03 um 16:19 schrieb Christoph M. Becker:
>>> Gregor Kofler wrote:
>>>
>>>> The following example should suffice to describe my problem:
>>>>
>>>> 50 if(!$file) {
>>>> 51 }
>>>> 52 else {
>>>> 53    echo '$file is an object.';
>>>> 54    var_dump(!$file);
>>>> 55 }
>>>>
>>>> results in
>>>>
>>>> "$file is an object.
>>>>
>>>> Catchable Fatal Error: Object of class
>>>> Symfony\Component\HttpFoundation\File\UploadedFile could not be
>>>> converted to boolean in
>>>> /var/www/strbase_client/src/Strbase/ClientBundle/Controller/BatchController.php
>>>> line 54"
>>>>
>>>> Why does the object to boolean conversion in the if condition in line 50
>>>> *not* cause an error?
>>>
>>> What gets printed when you change line 51 to:
>>>
>>>     var_dump($file);}
>>>
>>> I guess that answers your question.
>>
>>
>> Erm... no. Line 51 is not reached - hence I get precisely the same
>> output as above.
>> $file exists and is a UploadedFile instance.
>>
>>
>
> This works for me:
>
> class a {}
>
> $b = new a ();
>
> if (!$b)
>    {
>    }
> else
>    {
>      echo '$a is an object!';
>      var_dump (!$b);
>    }
>
> Maybe its an issue related to your specific class?

I suppose. I just found

https://bugs.php.net/bug.php?id=65213

...and the returned object has SplFileInfo on top of its class hierarchy.

Gregor

[toc] | [prev] | [next] | [standalone]


#14689

FromMatthew Carter <m@ahungry.com>
Date2014-12-03 13:13 -0500
Message-ID<878uioob4g.fsf@ahungry.com>
In reply to#14685
Gregor Kofler <usenet@gregorkofler.com> writes:

> Am 2014-12-03 um 17:42 schrieb Matthew Carter: > Gregor Kofler
><usenet@gregorkofler.com> writes:
>>
>>> Am 2014-12-03 um 16:19 schrieb Christoph M. Becker:
>>>> Gregor Kofler wrote:
>>>>
>>>>> The following example should suffice to describe my problem:
>>>>>
>>>>> 50 if(!$file) { 51 } 52 else { 53 echo '$file is an object.'; 54
>>>>> var_dump(!$file); 55 }
>>>>>
>>>>> results in
>>>>>
>>>>> "$file is an object.
>>>>>
>>>>> Catchable Fatal Error: Object of class
>>>>> Symfony\Component\HttpFoundation\File\UploadedFile could not be
>>>>> converted to boolean in
>>>>> /var/www/strbase_client/src/Strbase/ClientBundle/Controller/BatchController.php
>>>>> line 54"
>>>>>
>>>>> Why does the object to boolean conversion in the if condition in
>>>>> line 50 *not* cause an error?
>>>>
>>>> What gets printed when you change line 51 to:
>>>>
>>>>     var_dump($file);}
>>>>
>>>> I guess that answers your question.
>>>
>>>
>>> Erm... no. Line 51 is not reached - hence I get precisely the same
>>> output as above.  $file exists and is a UploadedFile instance.
>>>
>>>
>>
>> This works for me:
>>
>> class a {}
>>
>> $b = new a ();
>>
>> if (!$b) { } else { echo '$a is an object!'; var_dump (!$b); }
>>
>> Maybe its an issue related to your specific class?
>
> I suppose. I just found
>
> https://bugs.php.net/bug.php?id=65213
>
> ...and the returned object has SplFileInfo on top of its class
> hierarchy.
>
> Gregor

Wow filed as a feature request not a bug, seems silly.

I guess you'd have to write some boolean wrapper like:

function z ($o)
{
  return $o instanceof SplFileInfo || (bool) $o;
}

To get around it (or manually code each time you know it is a child of
SplFileInfo).

As a side note, on my php version (5.6.3) it *does* fail on the if check.

-- 
Matthew Carter (m@ahungry.com)
http://ahungry.com

[toc] | [prev] | [next] | [standalone]


#14683

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-12-03 16:46 +0000
Message-ID<m5neof$125$6@dont-email.me>
In reply to#14679
On Wed, 03 Dec 2014 15:56:23 +0100, Gregor Kofler wrote:

> The following example should suffice to describe my problem:
> 
> 50 if(!$file) {
> 51 }
> 52 else {
> 53    echo '$file is an object.';
> 54    var_dump(!$file);
> 55 }
> 
> results in
> 
> "$file is an object.

Try casting file to boolean:

54    var_dump(!(boolean)$file);

and see if that works as you expected.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#14686

FromGregor Kofler <usenet@gregorkofler.com>
Date2014-12-03 18:09 +0100
Message-ID<m5ng47$k3d$1@dont-email.me>
In reply to#14683
Am 2014-12-03 um 17:46 schrieb Denis McMahon:
> On Wed, 03 Dec 2014 15:56:23 +0100, Gregor Kofler wrote:
>
>> The following example should suffice to describe my problem:
>>
>> 50 if(!$file) {
>> 51 }
>> 52 else {
>> 53    echo '$file is an object.';
>> 54    var_dump(!$file);
>> 55 }
>>
>> results in
>>
>> "$file is an object.
>
> Try casting file to boolean:
>
> 54    var_dump(!(boolean)$file);
>
> and see if that works as you expected.
>
Without testing I assume it would yield the same result. Found a 
possible explanation here

https://bugs.php.net/bug.php?id=65213

it still doesn't explain why casting to boolean in the if condition 
doesn't throw an error.

[toc] | [prev] | [next] | [standalone]


#14687

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2014-12-03 18:22 +0100
Message-ID<m5ngtb$8dj$1@solani.org>
In reply to#14686
Gregor Kofler wrote:

> Am 2014-12-03 um 17:46 schrieb Denis McMahon:
>> On Wed, 03 Dec 2014 15:56:23 +0100, Gregor Kofler wrote:
>>
>>> The following example should suffice to describe my problem:
>>>
>>> 50 if(!$file) {
>>> 51 }
>>> 52 else {
>>> 53    echo '$file is an object.';
>>> 54    var_dump(!$file);
>>> 55 }
>>>
>>> results in
>>>
>>> "$file is an object.
>>
>> Try casting file to boolean:
>>
>> 54    var_dump(!(boolean)$file);
>>
>> and see if that works as you expected.
>>
> Without testing I assume it would yield the same result. Found a
> possible explanation here
> 
> https://bugs.php.net/bug.php?id=65213

See also <http://3v4l.org/U8JJ4#v520>.

> it still doesn't explain why casting to boolean in the if condition
> doesn't throw an error.

Indeed.  Might that be caused by some dubious optimization (e.g. by
OPcache)?

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#14688

FromGregor Kofler <usenet@gregorkofler.com>
Date2014-12-03 19:12 +0100
Message-ID<m5njqb$4cf$1@dont-email.me>
In reply to#14687
Am 2014-12-03 um 18:22 schrieb Christoph M. Becker:
> Gregor Kofler wrote:
>
>> Am 2014-12-03 um 17:46 schrieb Denis McMahon:
>>> On Wed, 03 Dec 2014 15:56:23 +0100, Gregor Kofler wrote:
>>>
>>>> The following example should suffice to describe my problem:
>>>>
>>>> 50 if(!$file) {
>>>> 51 }
>>>> 52 else {
>>>> 53    echo '$file is an object.';
>>>> 54    var_dump(!$file);
>>>> 55 }
>>>>
>>>> results in
>>>>
>>>> "$file is an object.
>>>
>>> Try casting file to boolean:
>>>
>>> 54    var_dump(!(boolean)$file);
>>>
>>> and see if that works as you expected.
>>>
>> Without testing I assume it would yield the same result. Found a
>> possible explanation here
>>
>> https://bugs.php.net/bug.php?id=65213
>
> See also <http://3v4l.org/U8JJ4#v520>.
>
>> it still doesn't explain why casting to boolean in the if condition
>> doesn't throw an error.
>
> Indeed.  Might that be caused by some dubious optimization (e.g. by
> OPcache)?

Indeed. Since my 5.5.9 shows the above behaviour but 5.4.35 already 
fails at line 50.

[toc] | [prev] | [next] | [standalone]


#14690

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2014-12-03 19:30 +0100
Message-ID<m5nkr6$mtk$1@solani.org>
In reply to#14688
Gregor Kofler wrote:

> Am 2014-12-03 um 18:22 schrieb Christoph M. Becker:
>> Gregor Kofler wrote:
>>
>>> it still doesn't explain why casting to boolean in the if condition
>>> doesn't throw an error.
>>
>> Indeed.  Might that be caused by some dubious optimization (e.g. by
>> OPcache)?
> 
> Indeed. Since my 5.5.9 shows the above behaviour but 5.4.35 already
> fails at line 50.

It seems I found the cause.  The if statement emits the opcodes
BOOL_NOT, JMPZ (according to VLD), what is optimized by OPcache to NOP,
JMPNZ[1].  So when the code is executed by OPcache, there is no
conversion involved for the if clause.  IMHO that makes changing the
behavior of SplFileInfo objects wrt. issue #65213 even more important.

[1] <http://lxr.php.net/xref/PHP_5_5/ext/opcache/Optimizer/block_pass.c#816>
[2] <https://bugs.php.net/bug.php?id=65213>

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#14691

FromGregor Kofler <usenet@gregorkofler.com>
Date2014-12-03 20:58 +0100
Message-ID<m5nq0s$uij$1@dont-email.me>
In reply to#14690
Am 2014-12-03 um 19:30 schrieb Christoph M. Becker:
> Gregor Kofler wrote:
>
>> Am 2014-12-03 um 18:22 schrieb Christoph M. Becker:
>>> Gregor Kofler wrote:
>>>
>>>> it still doesn't explain why casting to boolean in the if condition
>>>> doesn't throw an error.
>>>
>>> Indeed.  Might that be caused by some dubious optimization (e.g. by
>>> OPcache)?
>>
>> Indeed. Since my 5.5.9 shows the above behaviour but 5.4.35 already
>> fails at line 50.
>
> It seems I found the cause.  The if statement emits the opcodes
> BOOL_NOT, JMPZ (according to VLD), what is optimized by OPcache to NOP,
> JMPNZ[1].  So when the code is executed by OPcache, there is no
> conversion involved for the if clause.  IMHO that makes changing the
> behavior of SplFileInfo objects wrt. issue #65213 even more important.
>
> [1] <http://lxr.php.net/xref/PHP_5_5/ext/opcache/Optimizer/block_pass.c#816>
> [2] <https://bugs.php.net/bug.php?id=65213>
>
Interesting. Thanks for digging this out.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web