Jump to content
xisto Community
Sign in to follow this  
sonesay

Objects As Array How do you store a collection of objects inside an object?

Recommended Posts

I am trying to create a object of class A that stores a collection of objects of class B. The problem is I cannot figure out how to retain them as objects when added to the array of class A.

some code for better example.

Class A{var $arrayHere; function addItem(item) {   $arrayHere[] = item; }}Class B{ function doSomething() { }}

When I add some items to class A array and try and call the methods of class B it says its undefined. Obviously I am not using it correctly and the array in class A are no long objects of class B. Anyone know how to accomplish this effect? Put an example code as well if you do thanks

Share this post


Link to post
Share on other sites

Well, if that is the exact code used, then i am amazed that you didn't get a syntax error for the variable "item." Either way, i believe the appropriate way to do what i think you're trying to do is like this:

<?phpClass A {	var $arrayHere;	function addItem($item){	   $arrayHere[] = $item;	}}Class B {	function doSomething(){		print "Something...";	}}$x = new A;$x->arrayHere[] = new B;$x->arrayHere[0]->doSomething();?>
If this is not what you're trying to do, then i have misunderstood.

Share this post


Link to post
Share on other sites

Sorry about that truefusion. I did not post the exact code I just put the structure up there. At the time my code was being messed up abit because I was trying different things. I thought it would have been the same structure as yours except I was looping mine going through the array and I had mistakes on a few places. In the end its basically the same as your. I think I was getting the error of calling on a function on a non object because I didn't properly reference it.

Share this post


Link to post
Share on other sites

I think I was getting the error of calling on a function on a non object because I didn't properly reference it.

I got that error when i attempted to initiate class B using the addItem function; that is,
$x->addItem(new B);
I knew it was unnecessary to use that function to try and add an object into an array since any member that isn't stated otherwise becomes a public member of that class by default, but i tried it anyway to try and duplicate the problem on my side. If the array were to have been a private member, then i would have been forced to edit the structure you provided beyond its original state.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.