I’m using PHP. I have an array of objects, and would like to add an object to the end of it.
$myArray[] = null; //adds an element
$myArray[count($myArray) - 1]->name = "my name"; //modifies the element I just added
The above is functional, but is there a cleaner and more-readable way to write that? Maybe one line?
Just do:
You need to create the object first (the
newline) and then push it onto the end of the array (the[]line).You can also do this:
However I would argue that’s not as readable, even if it is more succinct.