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


Groups > comp.lang.php > #14689

Re: Consistency with object to boolean conversions

From Matthew Carter <m@ahungry.com>
Newsgroups comp.lang.php
Subject Re: Consistency with object to boolean conversions
Date 2014-12-03 13:13 -0500
Organization Ahungry (http://ahungry.com)
Message-ID <878uioob4g.fsf@ahungry.com> (permalink)
References <m5n8a7$gra$1@dont-email.me> <m5n9l9$djm$1@solani.org> <m5narg$rvn$1@dont-email.me> <87d280ofau.fsf@ahungry.com> <m5nfuo$jbu$1@dont-email.me>

Show all headers | View raw


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

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web