Re: get an object property

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

 



On 9/12/09 9:50 AM, "Tom Worster" <fsb@xxxxxxxxxx> wrote:

> On 9/12/09 1:32 AM, "Lars Torben Wilson" <torben@xxxxxxx> wrote:
> 
>> Tom Worster wrote:
>>> if i have an expression that evaluates to an object, the return value from a
>>> function, say, and i only want the value of one of the objects properties,
>>> is there a tidy way to get it without setting another variable?
>>> 
>>> to illustrate, here's something that doesn't work, but it would be
>>> convenient if it did:
>>> 
>>> $o = array( (object) array('a'=>1), (object) array('a'=>2) );
>>> 
>>> if ( end($o)->a > 1 ) {  // can't use -> like this!
>>> ...
>>> }
>> 
>> What version of PHP are you using? Your example should work.
>> 
>> Torben
> 
> 5.2.9.
> 
> what version does it work in?

i shamefully beg your pardon, lars. i was sure i tested the example but it's
clear to me now i either didn't or i made a mistake. end($o)->a IS php
syntax! so -> may follow a function (or method, i guess) call.

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.

but mine is a theoretical question about syntax, not a practical one. i'm
exploring php's syntactic constraints on the -> operator in contrast to,
say, the + or . operators. and in contrast to other languages.

for example, the . in js seems more generally allowed than -> (or, for that
matter, []) in php. programmers (especially using jquery) are familiar with
using . after an expression that evaluates to an object, e.g.

<body>
<p id="thepara" class="top x23 indent">My x class number is
    <span id="num"></span></p>
<div id="mandatory" style="border: solid red 1px"></div>
<script type="text/javascript">
document.getElementById('num').innerText =
  ( ( document.getElementById('optional')
      || document.getElementById('mandatory')
    ).appendChild(document.getElementById('thepara'))
    .className.match(/x(\d+)/) || [0,'absent']
  )[1]
</script>
</body>

which shows . after objects, method calls and expressions (as well as the []
operator applied to an expression).

do we just live without in phpville or am i missing something?


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?




-- 
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