Povylas
Povylas

Reputation: 786

Creating an array of objects in PHP

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();
    }
}

Upvotes: 6

Views: 58415

Answers (5)

Marcel
Marcel

Reputation: 5119

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();
}

Upvotes: 0

Emrul Chowdhury
Emrul Chowdhury

Reputation: 1

hope this will be helpful

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

Upvotes: -1

Byron Whitlock
Byron Whitlock

Reputation: 53851

Any of the following are valid:

$myArray = array();
$myArray[] = new Object();
$myArray[1] = new Object();
array_push($myArray, new Object);

Upvotes: 18

Chillie
Chillie

Reputation: 1026

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.

Upvotes: -3

Jim Jose
Jim Jose

Reputation: 1319

Try this,

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

or

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

EDIT: Class to stdClass

Upvotes: 5

Related Questions