6

I would like to know what is the right of creating objects arrays in php.
My goal here is to be able to get data like this:

$obj = new MyClass();
echo $obj[0]->parameter; //value1
echo $obj[1]->parameter; //value2

Thanks for your time.

EDIT: And if I want to do it in class it should look like this?

class MyClass{
    public $property;

    public function __construct() {
        $this->property[] = new ProjectsList();
    }
}
1
  • Are $obj[0] and $obj[1] instances of the same class? Commented Dec 9, 2011 at 20:22

5 Answers 5

18

Any of the following are valid:

$myArray = array();
$myArray[] = new Object();
$myArray[1] = new Object();
array_push($myArray, new Object);
Sign up to request clarification or add additional context in comments.

2 Comments

@Knittle - You are right. php needs more consistent parameter ordering.
@Byron Whitlock that's one opinion. However, if you look at the order of the parameters and the order of the function name, you'll find that they match sequentially. For example: array_push(x, y) x is the ARRAY, y is the item to be pushed, hence "array" is the first word and "push" is the second. in_array(x, y) x is the item to be checked for if it's "in" and y is the ARRAY. hence "in" is the first word, and "array" is the second. Perhaps that will help you remember more easily, it's something I have to check every time :)
5

Try this,

$obj = array(new stdClass(), new stdClass())

or

$obj = array()
$obj[] = new stdClass()
$obj[] = new stdClass()

EDIT: Class to stdClass

Comments

0

An important hint ...

The memory allocation for PHP arrays is exponential. A simple example: All array entries fit into an array, that allocates 2MB of memory. If this memory is no longer sufficient for all array entries, the memory allocation is expanded exponentially until the PHP memory limit is reached. If the array needs more than the 2 MB in this example, it will expand to 4MB, 16MB, etc.

When dealing with objects, better use collections. PHP provides the SplObjectStorage. This kind of collection allocates the exactly needed memory for its contents. Iteration over this collections behaves like using a yield. Only the memory for the current object in iteration is allocated. Beside that the SplObjectStorage class takes objects only. Should work perfectly for your purposes.

<?php
declare('strict_types=1');
namespace Marcel;

$collection = new SplObjectStorage();

$object1 = new stdClass();
$object2 = new stdClass();

$collection->attach($object1);
$collection->attach($object2);

The above shown code allows some stricter searching.

$collection->contains($object1); // returns true

Detaching an item works like a charme.

$collection->detach($object1);
$collection->contains($object1); // returns false

Iterations works with a pretty fast foreach loop or a while loop.

$collection->rewind();
foreach ($collection as $object) {
    // yielding works here
}

$collection->rewind();
while ($collection->valid()) {
    // yielding also works here automatically
    $collection->next();
}

Comments

-1

hope this will be helpful

$newArray[] = (object) array();

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post. stackoverflow.com/help/how-to-answer
-3

Honestly, I think you are on the right path. from what it sounds like you are not just trying to add to arrays, but convert arrays to objects.

<?php
 $obj = (object) 'ciao';
 echo $obj->scalar;  // outputs 'ciao'
 ?>

PHP Objects

EDIT: I don't think you could add an object like this:

  $this->property[] = new ProjectsList();

the "new ProjectsList()" would be how you would create an object from a class. ProjectsList would need to be a class. it would look more like this:

   $obj = new ProjectsList;
   $this->property[] = $obj;

you would need to make sure the ProjectsList existed first though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.