johnmerlino Posted March 27, 2011 Share Posted March 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231877-assigning-an-object-of-multiple-instances-to-another-objects-property/ Share on other sites More sharing options...
ignace Posted March 28, 2011 Share Posted March 28, 2011 $this->has_posts = (object) array_merge((array) $a, (array) $b, (array) $c); http://be.php.net/manual/en/language.oop5.php Quote Link to comment https://forums.phpfreaks.com/topic/231877-assigning-an-object-of-multiple-instances-to-another-objects-property/#findComment-1193179 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.