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


Groups > comp.lang.php > #6819

Re: json_decode problem

Message-ID <7187320.lErI1fH90z@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-08-20 16:45 +0200
Subject Re: json_decode problem
Newsgroups comp.lang.php
References <slrnk2avb8.be8.houghi@penne.houghi> <k055v2$62k$1@dont-email.me> <3488766.5KQix4pcSt@PointedEars.de> <k0t48i$8mk$1@dont-email.me>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


Curtis Dyer wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
>> FYI: isset() should not be used with properties, because it
>> fails with protected or private properties that have getters:
>> 
>> $ php -r 'class Foo { protected $_bar; public function
>> __construct() { $this->_bar = 42; } public function __get($name)
>> { return $this->_bar; } } $x = new Foo(); var_dump($x);
>> var_dump($x->foo); var_dump(isset($x->foo));' object(Foo)#1 (1)
>> { 
>>   ["_bar":protected]=>
>>   int(42)
>> }
>> int(42)
>> bool(false)
>> 
>> I have run into this problem recently in an MVC-based commercial
>> application that I had written, where I was accessing a template
>> variable (implemented as item of a protected array property of
>> the view class, accessed with $this->foo). Not using isset() in
>> the template would have saved me hours of debugging.
>> property_exists() would not work either from public context then
>> (it would always return FALSE).
> 
> According to the PHP documentation, as of PHP 5.3.0,
> `property_exists()' will also acknowledge protected and private
> properties. [1]

ACK:

$ php -r 'class Foo { private $bar; } var_dump(property_exists(new Foo(), 
"bar"));'
bool(true)

$ php -r 'class Foo { private $bar; } var_dump(property_exists(new Foo(), 
"baz"));'
bool(false)

$ php -r 'class Foo { protected $bar; } var_dump(property_exists(new Foo(), 
"bar"));'
bool(true)

$ php -r 'class Foo { protected $bar; } var_dump(property_exists(new Foo(), 
"baz"));'
bool(false)

$ php -v
PHP 5.3.10-1 (cli) (built: Feb  3 2012 10:03:01) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with XCache v1.3.2, Copyright (c) 2005-2011, by mOo
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
    with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH

One could consider that an OOP bug.  On the other hand, var_dump() also 
dumps private and protected properties from public context:

$ php -r 'class Foo { private $bar; } var_dump(new Foo());'object(Foo)#1 (1) 
{
  ["bar":"Foo":private]=>
  NULL
}

$ php -r 'class Foo { protected $bar; } var_dump(new Foo());'
object(Foo)#1 (1) {
  ["bar":protected]=>
  NULL
}

Either both functions are OOP-buggy, or none is.
 
> When using at least PHP 5.1.0, one might also consider PHP
> reflection to check for private and protected properties:
> 
>   <?php
> 
>   class Foo {
>     private $test;
>   }
> 
>   $r = new ReflectionObject(new Foo());
>   var_dump($r->hasProperty('test')); /* bool(true) */
> 
>   ?>

Overkill in most cases.


PointedEars
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

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


Thread

json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-10 23:30 +0200
  Re: json_decode problem Gregor Kofler <usenet@gregorkofler.com> - 2012-08-11 10:43 +0200
    Re: json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-11 13:09 +0200
    Re: json_decode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-12 13:44 +0200
      Re: json_decode problem Luuk <luuk@invalid.lan> - 2012-08-12 15:28 +0200
      Re: json_decode problem Luuk <luuk@invalid.lan> - 2012-08-12 15:32 +0200
      Re: json_decode problem Gregor Kofler <usenet@gregorkofler.com> - 2012-08-12 18:12 +0200
        Re: json_decode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-12 23:30 +0200
      Re: json_decode problem Curtis Dyer <dyer85@gmail.com> - 2012-08-20 10:41 +0000
        Re: json_decode problem Jerry Stuckle <jstucklex@attglobal.net> - 2012-08-20 07:30 -0400
        Re: json_decode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-20 16:45 +0200
  Re: json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-11 10:54 +0200
    Re: json_decode problem Luuk <luuk@invalid.lan> - 2012-08-11 11:05 +0200
      Re: json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-11 13:11 +0200

csiph-web