0

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.

3
  • First, why?!?! If you must, build a simple registry class maybe? Also, in PHP4 a function with the same name as the class is automatically called as the constructor. Commented Oct 28, 2013 at 19:58
  • @AbraCadaver Why do I want all of the objects stored in an array? Each object is a product on my client's site, and certain pages need to list all of their products. Commented Oct 28, 2013 at 20:03
  • So you built objects to represent specific products and the only time the app knows what products are available is when you manually instantiate them? I smell a serious design flaw here. Commented Oct 28, 2013 at 20:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

PHP 4 just started using Classes and Objects and you can't rely on PHP 4. Sammitch is right, you need an upgrade!
How would I get get the latest index of the $LIST once I push it on there because I need to change some properties as soon as it goes on there.
Nevermind. I just pushed each object onto $LIST and with the object I passed an array with all my variables. Thanks for the idea, @Sammitch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.