1
\$\begingroup\$

I've just stumbled upon something today that seemed "unethical" in PHP. What I originally thought as malpractice, turned out to work just fine. Basically what I was wondering is if pushing an instantiated class in an array was considered proper?

Example:

$array = array();

array_push($array, new myClass());

foreach($array as $i => $class)
{
    $class->doSomething();  
}

Although this example is quite vague, the idea is there. Any input or alternatives on this matter would be great.

\$\endgroup\$
1
\$\begingroup\$

That example looks like a very simple "chain of command" implementation. Looks fine to me, nothing unusual (though YYMV depending on the code-base).

You'll see things like this as an alternative to long blocks of procedural code, often when there's a bunch of distinct actions to perform that have no relation to one another. It's a nice way to clean up and cut up large classes to keep things "SRP"'d.

\$\endgroup\$
  • \$\begingroup\$ Great stuff. Reason for asking was due to the fact I've recently picked up PHP, migrating from Java where you'd see a lot of this stuff. \$\endgroup\$ – Dimitri Dec 10 '13 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.