Jump to content

[SOLVED] problem with agregate classes


freenity

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.