2
return array(
    'client_id' => $client[0]->getId(),
    'client secret' =>  $client[0]->getSecret(),
    'redirect_uri' =>  ($client[0]->GetClientEndpoints())[0]->getRedirectUri(),
    'name' =>  $client[0]->getName());

specifically the snippet:

($client[0]->GetClientEndpoints())[0]->getRedirectUri()

Is there a way I can do this without having to do this:

 $endpoints = $client[0]->GetClientEndpoints();
 return array(
    'client_id' => $client[0]->getId(),
    'client secret' =>  $client[0]->getSecret(),
    'redirect_uri' =>  $endpoints[0]->getRedirectUri(),
    'name' =>  $client[0]->getName());
1
  • 2
    This syntax was adding in PHP 5.4 (I believe it was 5.4). So, use PHP >= 5.4. Commented Mar 25, 2013 at 20:11

2 Answers 2

2

$client[0]->GetClientEndpoints()[0]->getRedirectUri() should work in PHP 5.4. Don't have 5.4 yet? You need to update.

It is very ugly though, and not more readable. I would:

$client = $client[0];
$endpoint = $client->GetClientEndpoints()[0];
return array(....
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I used, I sometimes forget the importance of readability
1

I'm not sure if it will work, but maybe you can try

http://www.php.net/manual/en/function.array-shift.php

return array(
    'client_id' => $client[0]->getId(),
    'client secret' =>  $client[0]->getSecret(),
    'redirect_uri' =>  array_shift($client[0]->GetClientEndpoints())->getRedirectUri(),
    'name' =>  $client[0]->getName());

At least this snippet works without any errors:

<?php
error_reporting(E_ALL);

class class1{
    public function getRedirectUri()
    {
        return 'something';
    }
}

$arr1 = array(new class1(), new class1());

$arr = array(
    'client_id' => 'something',
    'client secret' =>  'something',
    'redirect_uri' =>  array_shift($arr1)->getRedirectUri(),
    'name' =>  'something');

UPDATE (thanks Rudie)

As it turns out, the sample above is not identical to the case of the author, so a better example (which throws E_STRICT and therefore my answer should not be considered) is:

<?php
error_reporting(E_ALL);

class class2{
    public function getRedirectUri()
    {
        return 'something';
    }
}

class class1 {
    public function getArrays() {
        return array(new class2(), new class2());
    }
}
$var = new class1 ();

$arr = array(
    'client_id' => 'something',
    'client secret' =>  'something',
    'redirect_uri' =>  array_shift($var->getArrays())->getRedirectUri(),
    'name' =>  'something');

5 Comments

array_shift expects a reference (with &, you know), not a function result. This will produce a warning or notice.
hm, see above, I turned error_reporting = E_ALL and nothing with the sample above
oh, sorry, my bad, it's not identical to the authors question
yep, it throws E_STRICT with this, sorry
Nice test case! E_STRICT it is. For the perfectionists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.