FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » json_decode problem
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: json_decode problem [message #178889 is a reply to message #178888] Mon, 20 August 2012 11:30 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 8/20/2012 6:41 AM, Curtis Dyer wrote:
> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> wrote:
>
>> Gregor Kofler wrote:
>>
>>> Am 2012-08-10 23:30, houghi meinte:
>
> <snip>
>
>>> foreach($p->lines as $l) {
>>> echo $l->stage;
>>>
>>> if(isset($l->quote)) {
>>> echo $l->quote;
>>> }
>>>
>>> if(isset($l->chars) {
>>> foreach($l->chars as $c) {
>>> echo $c->char;
>>> echo $c->nconst;
>>> }
>>> }
>>>
>>> ....
>>> }
>>
>> 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]
>
> 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) */
>
> ?>
>
>
> <snip>
>
> ______
> [1] <http://php.net/property_exists>
>

You should not need either. Properties are private or protected for a
reason!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: PEAR Auth package woes
Next Topic: redirect on zend
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 19:21:21 GMT 2024

Total time taken to generate the page: 0.06530 seconds