Re: [PHP] get an object property

2009-09-13 Thread Lars Torben Wilson

Tom Worster wrote:

On 9/12/09 9:50 AM, Tom Worster f...@thefsb.org wrote:

  

On 9/12/09 1:32 AM, Lars Torben Wilson tor...@php.net 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 indentMy 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



Re: [PHP] get an object property

2009-09-13 Thread Tom Worster
On 9/13/09 3:21 AM, Lars Torben Wilson tor...@php.net wrote:

 On 9/12/09 9:50 AM, Tom Worster f...@thefsb.org 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



Re: [PHP] get an object property

2009-09-13 Thread Lars Torben Wilson

Tom Worster wrote:

On 9/13/09 3:21 AM, Lars Torben Wilson tor...@php.net wrote:

  

On 9/12/09 9:50 AM, Tom Worster f...@thefsb.org 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.

  
Yeah. . .never been a fan of the libs which return objects, although 
such a model does

perhaps have its uses.

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.

  


Yes, I'd say the same thing, except I'd replace the term 'static' with 
'declared'. If an
object is created on the fly, in someone else's code, and I have to 
maintain that code,
then either that code must be well-documented or I have to go on a hunt 
through
the source code to find out what might be available within that object. 
Not my idea
of fun. Convenient for the original coder, perhaps, especially if they 
come from an
automatic model background such as Javascript. And maybe one day I'll 
come to
love it. So far I haven't seen enough of a benefit to convince me that 
it's worth the
long-term maintenance headache it can (note that I say can, not 
does) cause.



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.

  
I also never found myself sold on the XML everywhere philosophy which 
seemed to
spring up during its first few years. I have found it useful for certain 
things--usually
involving documents. :) It's awesome for technical documentation such as 
working on
the PHP manual; I've used it when writing books; and XML and the DOM can 
be a
great help when constructing automatically validated XHTML. But there 
are also many
other things which people insisted it would be perfect for which just 
end up being a
waste of cycles and memory. It's a good tool for some tasks but 
completely ill-suited

for others IMHO.

I like the idea of json when working with Javascript. Years ago (before 
var_export())
I wrote something very similar to var_export() which would write out a 
human-readable
and directly PHP-parseable string for structured data. Sort of like. . 
.er. . .pson (*cough*). ;)

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.
  
Interesting. For something like that I would just use var_export() and 
skip the overhead
of parsing json back into PHP if I needed to do that. I'd use json when 
using 

Re: [PHP] get an object property

2009-09-12 Thread Tom Worster
On 9/12/09 12:31 AM, Paul M Foster pa...@quillandmouse.com wrote:

 On Fri, Sep 11, 2009 at 07:31:01PM -0400, 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!
 ...
 }
 
 
 You should use print_r() or var_dump() to investigate what happens when
 you try to cast an array into an object. I myself don't know what would
 happen. Also, what's allowed and what effects are produced could depend
 heavily on the version of PHP you're running. Version 4 != 5 != 5.3 in
 this respect.

i did that long ago when i was looking for php's object literal syntax. i
didn't find one. we discussed it here more recently and consensus appeared
to be that casting an array was the most convenient workaround.



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



Re: [PHP] get an object property

2009-09-12 Thread Tom Worster
On 9/12/09 1:32 AM, Lars Torben Wilson tor...@php.net 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?



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



Re: [PHP] get an object property

2009-09-12 Thread Tom Worster
On 9/12/09 9:50 AM, Tom Worster f...@thefsb.org wrote:

 On 9/12/09 1:32 AM, Lars Torben Wilson tor...@php.net 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 indentMy 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



Re: [PHP] get an object property

2009-09-12 Thread Ralph Deffke
 echo a()['q'];  // isn't?

because this is simply not valid syntax for the INTERPRETER PHP

while this
 echo o()-q;
can be interpreted because of the design of the interpreter.

I can live with that.

ralph_def...@yahoo.de

Tom Worster f...@thefsb.org wrote in message
news:c6d13522.12422%...@thefsb.org...
 On 9/12/09 9:50 AM, Tom Worster f...@thefsb.org wrote:

  On 9/12/09 1:32 AM, Lars Torben Wilson tor...@php.net 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 indentMy 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



RE: [PHP] get an object property

2009-09-12 Thread Jan Reiter
I have to agree, your example works fine for me. For testing I used the
latest stable Release of server2go with PHP 5.2.10.

Or do you want to use something like that:

class o
{
protected $arr = array('a'=0);
function o($n){$this-arr['a'] = $n;}
function getA(){return $this-arr['a'];}
}

$o = array( new o(1), new o(2) );


if ( end($o)-getA()  1 ) {  
echo yeah;
}

Which works fine as well ... 

Regards,
jan

-Original Message-
From: Tom Worster [mailto:f...@thefsb.org] 
Sent: Saturday, September 12, 2009 1:31 AM
To: PHP General List
Subject: [PHP] get an object property

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!
...
}




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Eingehende eMail ist virenfrei.
Von AVG überprüft - www.avg.de 
Version: 8.5.409 / Virendatenbank: 270.13.71/2336 - Ausgabedatum: 09/11/09
09:15:00 


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



[PHP] get an object property

2009-09-11 Thread Tom Worster
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!
...
}




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



Re: [PHP] get an object property

2009-09-11 Thread John Corry

Wouldn't it be $o[1]-a?

J Corry
Sent from my iPhone

On Sep 11, 2009, at 7:31 PM, Tom Worster f...@thefsb.org 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!
...
}




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



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



Re: [PHP] get an object property

2009-09-11 Thread Paul M Foster
On Fri, Sep 11, 2009 at 07:31:01PM -0400, 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!
 ...
 }
 

You should use print_r() or var_dump() to investigate what happens when
you try to cast an array into an object. I myself don't know what would
happen. Also, what's allowed and what effects are produced could depend
heavily on the version of PHP you're running. Version 4 != 5 != 5.3 in
this respect.

Paul

-- 
Paul M. Foster

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



Re: [PHP] get an object property

2009-09-11 Thread Lars Torben Wilson

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

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