Re: [PHP-DEV] Array access on function returns

2008-04-19 Thread Arvids Godjuks
2008/4/18, Sam Barrow [EMAIL PROTECTED]:

 On Fri, 2008-04-18 at 10:40 -0700, Kalle Sommer Nielsen wrote:
  Hey Internals
 
  I've been wondering for quite some time why PHP doesn't allow you to
 access
  arrays when you assign it to a value like in Javascript:
 
  function ArrayTest(Array $range)
  {
return($range);
  }
 
  $range = Array(1337 = Array('Hello World'));
 
  echo ArrayTest($range)[1337};

 I was just about to write an email asking this same exact question this
 afternoon. This could be very useful, I don't see any reason not to have
 it.

 
  I would really like to see this introduced in PHP sometime (Perhaps as
  an extra addition to 5.3's new and sleek features).
 
  This is very useful in a few areas and can save a few lines of code
  here and there.
 
  unfortunately Im not a C Programmer myself else I've would have
  provided a patch for this =/
 
 
  Cheers
 
  Kalle Sommer Nielsen
  Zend Certified Engineer
 
 


 --
 PHP Internals - PHP Runtime Development Mailing List
 To unsubscribe, visit: http://www.php.net/unsub.php


You now, PHP has it's own way of implementing this - just use list()

function ArrayTest(Array $range) {
  return($range);
}

$range = Array('Hello World', 'Hello Me!');
list( , $greetMe) = ArrayTest($range);
echo $greet;

Although you example can't be implemented via list() because you key has
insane value, but usualy we are stuck with few keys with sensible values
like 0, 1, 2, 3. And you should realy return a string then, not an array. I
never had a situation where list() couldn't help. And don't return a big
array because of one value - that will help saving memory :) That's against.

Well, I have an argument also for this one.
It could realy help then returning multidemensional arrays or if an array of
objects is returned

// $object is some object witch contains a lot of other objects in it's
properties
function getObjectList($object) {
$objectList = array();
foreach ($object as $value){
if (is_object($value)){
$objectList[get_class($value)] = $value;
}
}
return $objectList;
}

// Let's assume we know for sure that object with name we know exists
$myObject = new SomeCoolObject();
$result = getObjectList($myObject)['db']-query($sql)-execute();

Well, a stupid one example. So to me it is a little pointless, because you
just can't check if  key exists and that will be a warning. So it is very
disputeable if we need it.


Re: [PHP-DEV] Array access on function returns

2008-04-19 Thread Kalle Sommer Nielsen

Hi Arvids

Quoting Arvids Godjuks [EMAIL PROTECTED]:


2008/4/18, Sam Barrow [EMAIL PROTECTED]:


On Fri, 2008-04-18 at 10:40 -0700, Kalle Sommer Nielsen wrote:
 Hey Internals

 I've been wondering for quite some time why PHP doesn't allow you to
access
 arrays when you assign it to a value like in Javascript:

 function ArrayTest(Array $range)
 {
   return($range);
 }

 $range = Array(1337 = Array('Hello World'));

 echo ArrayTest($range)[1337};

I was just about to write an email asking this same exact question this
afternoon. This could be very useful, I don't see any reason not to have
it.


 I would really like to see this introduced in PHP sometime (Perhaps as
 an extra addition to 5.3's new and sleek features).

 This is very useful in a few areas and can save a few lines of code
 here and there.

 unfortunately Im not a C Programmer myself else I've would have
 provided a patch for this =/


 Cheers

 Kalle Sommer Nielsen
 Zend Certified Engineer




--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



You now, PHP has it's own way of implementing this - just use list()

function ArrayTest(Array $range) {
  return($range);
}

$range = Array('Hello World', 'Hello Me!');
list( , $greetMe) = ArrayTest($range);
echo $greet;


I think it would be more suitable to have the array access syntax insted of
the list(, , , $value) syntax, it helps readability for example.



Although you example can't be implemented via list() because you key has
insane value, but usualy we are stuck with few keys with sensible values
like 0, 1, 2, 3. And you should realy return a string then, not an array. I
never had a situation where list() couldn't help. And don't return a big
array because of one value - that will help saving memory :) That's against.

Well, I have an argument also for this one.
It could realy help then returning multidemensional arrays or if an array of
objects is returned

// $object is some object witch contains a lot of other objects in it's
properties
function getObjectList($object) {
$objectList = array();
foreach ($object as $value){
if (is_object($value)){
$objectList[get_class($value)] = $value;
}
}
return $objectList;
}

// Let's assume we know for sure that object with name we know exists
$myObject = new SomeCoolObject();
$result = getObjectList($myObject)['db']-query($sql)-execute();

Well, a stupid one example. So to me it is a little pointless, because you
just can't check if  key exists and that will be a warning. So it is very
disputeable if we need it.



I think the usage of this would be in cases where you know the key exists, for
example on getimagesize():

if(!getimagesize($some_image_url)[2] == IMAGETYPE_PNG)
{
die('Please supply a valid url to a png image...');
}

Theres quite alot functions where you need to only get one key and insted of
doing the list() (as written above) it would be nicer with syntax.


Kalle Sommer Nielsen
Zend Certified Engineer


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Array access on function returns

2008-04-19 Thread Larry Garfield
On Saturday 19 April 2008, Kalle Sommer Nielsen wrote:
 Hi Arvids

 Quoting Arvids Godjuks [EMAIL PROTECTED]:
  2008/4/18, Sam Barrow [EMAIL PROTECTED]:
  On Fri, 2008-04-18 at 10:40 -0700, Kalle Sommer Nielsen wrote:
   Hey Internals
  
   I've been wondering for quite some time why PHP doesn't allow you to
 
  access
 
   arrays when you assign it to a value like in Javascript:
  
   function ArrayTest(Array $range)
   {
 return($range);
   }
  
   $range = Array(1337 = Array('Hello World'));
  
   echo ArrayTest($range)[1337};
 
  I was just about to write an email asking this same exact question this
  afternoon. This could be very useful, I don't see any reason not to have
  it.
 
   I would really like to see this introduced in PHP sometime (Perhaps as
   an extra addition to 5.3's new and sleek features).
  
   This is very useful in a few areas and can save a few lines of code
   here and there.

  // Let's assume we know for sure that object with name we know exists
  $myObject = new SomeCoolObject();
  $result = getObjectList($myObject)['db']-query($sql)-execute();
 
  Well, a stupid one example. So to me it is a little pointless, because
  you just can't check if  key exists and that will be a warning. So it is
  very disputeable if we need it.

 I think the usage of this would be in cases where you know the key exists,
 for example on getimagesize():

 if(!getimagesize($some_image_url)[2] == IMAGETYPE_PNG)
 {
   die('Please supply a valid url to a png image...');
 }

 Theres quite alot functions where you need to only get one key and insted
 of doing the list() (as written above) it would be nicer with syntax.


 Kalle Sommer Nielsen
 Zend Certified Engineer

It is also useful if returning an object that implements ArrayAccess and 
lazy-loads data.  

$name = load_something($id)['name'];

If using lazy-loading internally, that could offer very cheap access to the 
name property of the something object, with only a tiny bit of code.  And 
it's really no more error prone than:

$something = load($id);
$name = $something['name'];

Presumably you're doing some sort of automated error handling either way 
(trigger, exceptions, whatever.)

-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   ICQ: 6817012

If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it.  -- Thomas 
Jefferson

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Array access on function returns

2008-04-19 Thread Mike Lively
On Fri, Apr 18, 2008 at 10:40 AM, Kalle Sommer Nielsen [EMAIL PROTECTED]
wrote:

 Hey Internals

 I've been wondering for quite some time why PHP doesn't allow you to
 access
 arrays when you assign it to a value like in Javascript:


My opinion on features such as this is that they provide too much
opportunity to do things in a far less than optimal way. Another feature
that was brought up (allowing method calls directly on the constructor) has
the same problem.

I realize that this type of functionality seems appealing when you need to
operate on only one index of an array or when you only want to make a single
method call on a newly instantiated object. But it is going to do it at the
cost of readability (imo) and in some cases reduce the speed or flexibility
of the code. If you reference the 'index' of the return value of a function
you in effect have lost access to all other indexes in the array. So, as
soon as you need another index you are forced to either rewrite your code to
not access the method directly as an array, or you have to make another full
function call to pull in the value. The same holds true for chaining
function calls onto constructors.

On the other hand it would be fun trying to track down exactly what you
should be expecting from: $var = new
Class()-call1()-property[0]-call2()[1]-wheee();



 function ArrayTest(Array $range)
 {
return($range);
 }

 $range = Array(1337 = Array('Hello World'));

 echo ArrayTest($range)[1337};


 I would really like to see this introduced in PHP sometime (Perhaps as an
 extra addition to 5.3's new and sleek features).

 This is very useful in a few areas and can save a few lines of code here
 and there.

 unfortunately Im not a C Programmer myself else I've would have provided a
 patch for this =/


 Cheers

 Kalle Sommer Nielsen
 Zend Certified Engineer


 --
 PHP Internals - PHP Runtime Development Mailing List
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP-DEV] Array access on function returns

2008-04-18 Thread Sam Barrow
On Fri, 2008-04-18 at 10:40 -0700, Kalle Sommer Nielsen wrote:
 Hey Internals
 
 I've been wondering for quite some time why PHP doesn't allow you to access
 arrays when you assign it to a value like in Javascript:
 
 function ArrayTest(Array $range)
 {
   return($range);
 }
 
 $range = Array(1337 = Array('Hello World'));
 
 echo ArrayTest($range)[1337};

I was just about to write an email asking this same exact question this
afternoon. This could be very useful, I don't see any reason not to have
it.

 
 I would really like to see this introduced in PHP sometime (Perhaps as  
 an extra addition to 5.3's new and sleek features).
 
 This is very useful in a few areas and can save a few lines of code  
 here and there.
 
 unfortunately Im not a C Programmer myself else I've would have  
 provided a patch for this =/
 
 
 Cheers
 
 Kalle Sommer Nielsen
 Zend Certified Engineer
 
 


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php