PHP syntax for dereferencing function result

2018-12-31 04:45发布

Background

In every other programming language I use on a regular basis, it is simple to operate on the return value of a function without declaring a new variable to hold the function result.

In PHP, however, this does not appear to be so simple:

example1 (function result is an array)

<?php 
function foobar(){
    return preg_split('/\s+/', 'zero one two three four five');
}

// can php say "zero"?

/// print( foobar()[0] ); /// <-- nope
/// print( &foobar()[0] );     /// <-- nope
/// print( &foobar()->[0] );     /// <-- nope
/// print( "${foobar()}[0]" );    /// <-- nope
?>

example2 (function result is an object)

<?php    
function zoobar(){
  // NOTE: casting (object) Array() has other problems in PHP
  // see e.g., http://stackoverflow.com/questions/1869812
  $vout   = (object) Array('0'=>'zero','fname'=>'homer','lname'=>'simpson',);
  return $vout;
}

//  can php say "zero"?       
//  print zoobar()->0;         //  <- nope (parse error)      
//  print zoobar()->{0};       //  <- nope                    
//  print zoobar()->{'0'};     //  <- nope                    
//  $vtemp = zoobar();         //  does using a variable help?
//  print $vtemp->{0};         //  <- nope     

Can anyone suggest how to do this in PHP?

22条回答
唯独是你
2楼-- · 2018-12-31 05:22

This is specifically array dereferencing, which is currently unsupported in php5.3 but should be possible in the next release, 5.4. Object dereferencing is on the other hand possible in current php releases. I'm also looking forward to this functionality!

查看更多
一个人的天荒地老
3楼-- · 2018-12-31 05:23

my usual workaround is to have a generic function like this

 function e($a, $key, $def = null) { return isset($a[$key]) ? $a[$key] : $def; }

and then

  echo e(someFunc(), 'key');

as a bonus, this also avoids 'undefined index' warning when you don't need it.

As to reasons why foo()[x] doesn't work, the answer is quite impolite and isn't going to be published here. ;)

查看更多
牵手、夕阳
4楼-- · 2018-12-31 05:26

These are some ways to approach your problem.

First you could use to name variables directly if you return array of variables that are not part of the collection but have separate meaning each.

Other two ways are for returning the result that is a collection of values.

function test() {
  return array(1, 2);
}   
list($a, $b) = test();
echo "This should be 2: $b\n";

function test2() {
   return new ArrayObject(array('a' => 1, 'b' => 2), ArrayObject::ARRAY_AS_PROPS);
}
$tmp2 = test2();
echo "This should be 2: $tmp2->b\n";

function test3() {
   return (object) array('a' => 1, 'b' => 2);
}
$tmp3 = test3();
echo "This should be 2: $tmp3->b\n";
查看更多
无色无味的生活
5楼-- · 2018-12-31 05:27

Previously in PHP 5.3 you had to do this:

function returnArray() {
  return array(1, 2, 3);
}
$tmp = returnArray();
$ssecondElement = $tmp[1];

Result: 2

As of PHP 5.4 it is possible to dereference an array as follows:

function returnArray() {
  return array(1, 2, 3);
}
$secondElement = returnArray()[1];

Result: 2

As of PHP 5.5:

You can even get clever:

echo [1, 2, 3][1];

Result: 2

You can also do the same with strings. It's called string dereferencing:

echo 'PHP'[1];

Result: H

查看更多
登录 后发表回答