I apologize, I have dabbled with some PHP, but never done anything serious until now.
Basically, I have something like this:
class MyClass
{
function _construct($var1, $var2)
{
//property assignments
}
}
function doStuff($array)
{
//do stuff to $array
}
$myArray = array();
$myVar1 = "foo";
$myVar2 = "bar";
$myVar3 = "test"
$myVar4 = "aaaaa"
//etc...
What I want to do is create an instance of MyClass and store it into $myArray. I know I can do the following:
$classInstance = new MyClass($myVar1, $myVar2);
$myArray[] = $classInstance
$anotherInstance = new MyClass($myVar3, $myVar4);
$myArray[] = $anotherInstance
//etc...
This process will be done multiple times (with no loops) sequentially. Once I have done this multiple times, I will be doing the following:
doStuff($myArray);
Inside this function, I will be looping through with a foreach, etc. This is going to be tedious. What I want to know, is there some way that I can do something like (I know this isn’t proper syntax):
$myArray[] = new MyClass($myVar1, $myVar2);
$myArray[] = new MyClass($myVar3, $myVar4);
//etc...
doStuff($myArray);
Any help would be appreciated. Thanks!
The syntax
$myArray[] = new MyClass($myVar3, $myVar4);is valid already. You can do exactly as you want to: