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.

Link to comment
Share on other sites

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.