Jump to content

assigning an object of multiple instances to another object's property


johnmerlino

Recommended Posts

Hey all,

 

I want to have an object that has a property which is an object containing instances of other objects. I try this:

class Blog extends Posts {
	public $has_posts;

	public function __construct($a,$b,$c){
		$has_posts = (object) array_merge((array) $a, (array) $b, (array) $c);
	}
}

class Posts {
	public $b;

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

$post1 = new Posts(1);
$post2 = new Posts(2);
$post3 = new Posts(3);

$blog = new Blog($post1,$post2,$post3);
var_dump($blog->has_posts); //null
foreach($blog->has_posts as $post){ //Invalid argument supplied for foreach()
	echo $post->b;
}

 

But as you see, has_posts is null, not an object containing other objects.

 

Thanks for response.

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.