I want to create an array within an object (handler) that holds a series of objects (subject) in PHP. The array is a property of the handler and I have a method to create new subjects.
class MyHandler (
$TheList = array();
$TempSubject = object; // class subject
public function AddNewSubject($information) {
$TempSubject = new subject($information);
$This->TheList [] = $TempSubject;
}
)
If I create a new subject as above, does the information persist object persist within MyHandler or is it lost after AddNewSubject ends? I am new to PHP so please comment on any errors.
$TempSubjectwithin the object’s method is only a temporary variable. If, however, you were to define your function like this:then the object’s property (
$this->TempSubject) would be updated each time but a copy of that object would be stored in$this->TheList.Finally, if you were to define your function like this:
you would find that
$this->TheListwould contain a list of references to the same object, which would be overridden every time you call the function.I hope that helps.