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 will look something like this: private ArrayList<Object> ls and the constructor will have this ls = new ArrayList<Object>();

thanks

+4
source share
2 answers

PHP allocates memory dynamically and, whatโ€™s more, it doesnโ€™t matter which object you store in your array.

If you want to declare an array before using it, follow these steps:

 var $myArray = array(); 

Then you can save any object you like in the $ myArray variable. Many people find this strange concept understandable after working in a strict language such as java.

+5
source

you can declare it in a class, for example

 private $array = array(); 

and add objects (or something) to this array, for example

 $array[] = some object 
+1
source

Source: https://habr.com/ru/post/1389658/


All Articles