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 #178888 is a reply to message #178844] Mon, 20 August 2012 10:41 Go to previous messageGo to previous message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma:
Member
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>

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
[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: Sun Nov 10 16:36:58 GMT 2024

Total time taken to generate the page: 0.04302 seconds