Jump to content

How to create object array in php


Antrikssh

Recommended Posts

Hello Everyone, I have to create object array for my userdefine class. In c++ when we create class and we want to create object array then we simply do by following statement. // My class in c++ class MyClass { public: int iVal;} if we want to create object array you do like that in c++ MyClass obj[5]; In this we create 5 object of class MyClass. I want to do same thing in php. Thanks and Regards,Antrikssh...

Link to comment
Share on other sites

there is a PHP forum for PHP questions. The Web Server forum is probably not the best place. Also, there are tutorials for PHP, of which there are topics specific to arrays.http://www.w3schools.../php_arrays.asp Without getting into the detail of classes in PHP. a simple way to push items to an array in PHP would be something like this.

$objects = array(); $objects[] = new MyClass();$objects[] = new MyClass();$objects[] = new MyClass();$objects[] = new MyClass();$objects[] = new MyClass(); echo count($objects) . ' made';  //5 objects made //use any object$objects[0]->someMethod();$objects[1]->someMethod();etc

Edited by thescientist
Link to comment
Share on other sites

Hello Sir, Yes you are right, I have post wrong topic on wrong place. I have to post this post on php question related section not in web server section. I apologies for this. But sir method that you describe is array of object not the object array. We do the same thing that you done in yor post like $object = array();for($i = 0; $i < 5; $i++) {$object = new MyClass();} after this we will get the same thing that you got array of 5 object. which is not my requirment. Thanks and Regards,Antrikssh....

Link to comment
Share on other sites

your example is just going to constantly overwrite $object. secondly, the point I was trying to make is that you can't do what you want in PHP. Thirdly, just ask a moderator to move your post. There is no reason to make two posts.http://w3schools.invisionzone.com/index.php?showtopic=45497

Link to comment
Share on other sites

hi, Yes you are right i made a mistake in my example i.e (1)$object = array();for($i = 0; $i < 5; $i++) {$object = new MyClass();} (2) How I will delete my one post and how I will communicate with the moderator.plz help me

Link to comment
Share on other sites

if you hover over a post, a report link will appear that will let you send a message to the moderators. I already sent them one for now.

Link to comment
Share on other sites

I moved the topic. But I don't understand exactly what you want. An "object array" really is just an array of objects. The only difference is that the objects are forced to be of only one type.

Link to comment
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
×
×
  • Create New...