user1114
user1114

Reputation: 1169

Declare an array of objects in php

How can I declare a list of objects in php as a private instance variable?

In Java the declaration would look something like this private ArrayList<Object> ls and the constructor would have this ls = new ArrayList<Object>();

Thanks

Upvotes: 6

Views: 31641

Answers (2)

James Ravenscroft
James Ravenscroft

Reputation: 375

PHP allocates memory dynamically and what's more, it doesn't care what sort of object you store in your array.

If you want to declare your array before you use it something along these lines would work:

var $myArray = array();

Then you can store any object you like in your variable $myArray. Many people find this a strange concept to grasp after working in a strict language like java.

Upvotes: 5

boobiq
boobiq

Reputation: 3024

you can declare it in class like

private $array = array();

and append objects (or anything) to that array like

$array[] = some object

Upvotes: 1

Related Questions