Referencing returned array elements...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Probably hvaing a major brain-cramp, but here's my problem.

Have a method to a 'word' object, parent() that returns an array of 'wrod'
objects that are parents of the current object.  i.e.



        [0]=>word Object([word]=>test [syllables]=>1)
        [1]=>word Object([word]=>testing [syllables]=>2)

Anyways, trying to reference the array elements when calling the function
directly, but not sure it's possible.

Here's how I can get it to work:

$temp_var = $word->parent();

then I can reference $temp_var[0] to get that specific object.

I WOULD like to 'chain' the statement a little more though, ala jquery, so I
can do more in less space, i.e.


So I can then do perform other actions on the selected item in the same
line, i.e.



$word->parent()[0] is causing an error of course, just wondering if there is
another way to refernce the elements directly after the function call.


Re: Referencing returned array elements...

Quoted text here. Click to load it

A quick follow-up...

$temp_var = $word->parent(); then.

$temp_var[0]->word produces the desired result in that the first word is
output, but I am getting the following error with E_STRICT:

PHP Notice:  Trying to get property of non-object

So obvioulsy I am not even doing the current referencing properly...  help!

Re: Referencing returned array elements...

GS escribió:
Quoted text here. Click to load it

I may be wrong but it don't think the PHP syntax allows such thing. The
[] operator only works on variables (strings and arrays).

Quoted text here. Click to load it

You'll probably have to add an extra parameter to parent(): the index of
the child you want to return:


class word{
    public $name;

    public function __construct($name){
        $this->name = $name;

    public function parent($index){
        $a = array(
            new word('Zero'),
            new Word('One'),

        return $a[$index];

    public function children(){
        echo 'Children of ' . $this->name;

$word = new word('Grandparent');
echo $word->parent(0)->children() . PHP_EOL;


-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor al baño María:

Re: Referencing returned array elements...

Quoted text here. Click to load it

Thanks, this is what I started thinking after my post, thanks for your
response it is appreciated!


Site Timeline