0

I am wondering why this doesnt work.

As of at least PHP 5.3, a function or class method returning an object acts like an object.

<?php

class A {
function test() {
    echo "Yay!";
    }
}

function get_obj() {
    return new A();
}


function get_array() {
    return array("foo", "bar", "hallo", "world");
}

get_obj()->test();  // "works
echo get_array()[1]; // and this fails 
?>

I found this at http://php.net/manual/en/functions.returning-values.php

would someone clarify why it doesn't work for arrays but works for objects.

EDIT:

added get_array()

2
  • 1
    do you know that you haven't posted the get_array() code? Commented Nov 29, 2012 at 16:02
  • I forgot to add it i will add it now Commented Nov 30, 2012 at 9:00

1 Answer 1

3

function array dereferencing was added in PHP 5.4, so unless you're on 5.4+, you have to do

$temp = get_array();
echo $temp[0];
Sign up to request clarification or add additional context in comments.

1 Comment

thanks this is what i was looking for i did not know this (i am running 5.3.13 so that was my problem good to know that this has been implemented)

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.