Question

I'm pretty new to OOP, but I have a class, and with each object created, I'm pushing them into an array (I don't think there's a way to iterate over every object in a class). My client is still on PHP4, so I was having some trouble.

Since version 4 doesn't come with the __construct method, I made my own:

function construct() {
    global $LIST;
    array_push($LIST, &$this);
}

Without the & before $this, it's just adding an empty object into the array because after it's instantiated, I change some properties for each object. Now it's throwing me a warning, though:

Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of array_push(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file.

I tried to just suppress the warning, but a @ in front of the array_push() doesn't work. And apparently the error_reporting(0) won't work unless it's in my root file (all of this stuff is inside of an include on many, many pages). I don't have access to the php.ini file either.

Was it helpful?

Solution

I find it hard to believe that it only works with &$this as the error message states that it is passing the object by value [aka, as $this] anyways.

Additionally, global in a class declaration? Bad. Horribad. Why are you not doing the following instead?

$LIST[] = new MyObject();

And if PHP4 is so archaic that the $arr[] syntax is not yet valid, then:

array_push($LIST, new MyObject());

Also, holy god. PHP4? Upgrade it. Seriously. I don't think you can possibly come up with a valid reason to still be using PHP4 at this point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top