Re: get an object property

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 9/13/09 3:21 AM, "Lars Torben Wilson" <torben@xxxxxxx> wrote:

>> On 9/12/09 9:50 AM, "Tom Worster" <fsb@xxxxxxxxxx> wrote:
>>
>> but let me give you a more different example:
>> 
>> $a and $b are normally both objects, each with various members including a
>> prop q, but sometimes $a is false. i want the q of $a if $a isn't false,
>> otherwise that of $b.
>> 
>> ($a ? $a : $b)->q   // is not php, afaik
>> 
>> before you suggest one, i know there are simple workarounds.
>>   
> You're right, that isn't PHP syntax. One workaround that came to mind
> which does
> a similar thing (although using a different mechanism) is this:
> 
>   ${$a ? 'a' : 'b'}->q

i would not have thought of that. interesting...


>> and while i'm at it, and using my original error, how come...
>> 
>> function o() { return (object) array('q'=>7); }
>> echo o()->q;  // is ok syntax, but
>> 
>> function a() { return array('q'=>5); }
>> echo a()['q'];  // isn't?
>>   
> I'm afraid I can't answer that right now--it does perhaps seem
> inconsistent at first glance,
> although I can't say I've ever missed it or felt that using syntax like
> that would make my
> life any better. Maybe it would. Then again, I can also see an argument
> being made for
> allowing the object syntax but not the array syntax: in the case of
> objects, you can have
> a clean class declaration which is pretty much self-documenting, and
> later users of the
> class can have a clear idea of which properties are available and which
> are not, and they
> can thus be sure that o()->q will not result in uninitialized property
> problems. Using the
> array syntax you could never be sure that the index requested actually
> exists.
> 
> Of course, this holds true only for people like me who don't really like
> the idea of creating
> objects on the fly in PHP unless there's a very good reason to. Usually
> in PHP such tasks
> are better handled by arrays anyway.

the dbms abstraction library i use delivers rows, by default, as objects. so
i commonly handle dynamically generated data in the form of objects, though
it's not my code generating those objects. i think that's one reasons why i
often find i would use objects as data structures. and because i find the
dynamic objects in js convenient.

but i think you're preference reflects more closely was probably the concept
of php's version of oop: an object is an instances of a static class.

in any case, now that i've confirmed that i'm not merely unaware of the
features i was hunting for, and that they don't exist by design, i can
perhaps move on.

on a related note, way back when xml was ascendant as "the most exciting new
technology to hit the net since java", i was not impressed. what a horrid
syntax for specifying and communicating data, i would argue. why not use the
syntax from some sensible programming language instead? js, for example?
easy to parse, less overhead, human readable (i find xml hard to read), etc.
then eventually json happened, without all the hype and fanfare, just doing
the job very conveniently. i love it.

and to make that comment vaguely php related, i now use json to encode
structured data that i want to write to the php error log.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux