Jump to content

Recommended Posts

Well actually I have 2 classes:

class a
{
private $bb = array();

public function __construct()
{
	for($i=0;$i<3;$i++)
		$this->bb[] = new b;		
}

public function getb()
{
	return $this->bb;
}
}


class b
{
public function hello()
{
	echo "hi";
}
}

 

the i do:

 

$aa = new a;

$aa->getb()[0]->hello();

 

and yes, it trows an error: Parse error: syntax error, unexpected '[' in D:\XAMPP\xampp\htdocs\test\index.php on line 39

I know that I can just store the value in a variable and then use it to call the function, but isn't it possible to do this?

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/70380-solved-problem-with-agregate-classes/
Share on other sites

I am stumped. Like you said,

 

$x = $aa->getb();

$x[0]->hello();

 

works fine.

 

This is a strange one, maybe it has something to do with the order of execution.

 

What is the reason why you want to use this format instead of setting it to a variable?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.