Re: get an object property

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

 



Tom Worster wrote:
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.
No need for apologies. :)
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

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.
I can respect that. This kind of exploration is often quite illuminating.
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?
We live without, just like we live without

$foo =~ s/bar/baz/i;

. . .and without:

cout << "Hello " << world << endl;

. . .and without:

#define FOO(bar, baz) ((bar) * (baz))

. . .and so on. It's just syntax from other languages which isn't part of the PHP syntax.
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.

This is of course just random thinking and perhaps it's just because nobody has added
that syntax to the scanner. :)

Anyway, good luck with your language comparison. Like I said before, that kind of thing
is often fun (and always instructional).


Regards,

Torben

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