DEV Community

loading...

Discussion on: PHP is bad for Object-Oriented Programming OOP

Collapse
jorgecc profile image
Jorge Castro Author • Edited

First of all using var to declare properties on Classes is a no no. Also I'd like to point out that you are breaking encapsulation principle of OOP in your examples. Accessor modifiers exist with a reason (public, protected, private) you should've used them instead of var.

Because it's way worse.

First, setter and getters are optimized by the JavaVM (and .net runtime engine) but not by PHP, so in PHP setter and getters are simple methods, i.e. it adds overhead. But let's say we use it.

How we could read a JSON rest (that supplies 1000 rows of data) and store this information into an OOP without killing the performance of the system? If we use setter and getter, then we should read each and every single row creates a new object, parse it and for what, for more code and slowness?. Why we should do that?. OOP?.

Something like that:


$js=json_decode(file_get_contents("\\somejson.json")); // it returns 1000 rows
$objects=[];
$fields=["field1","field2"...]; // 10 fields
foreach($js as $item) { // 1000 times
  $obj=new SomeClass();
  for($fields as $field) {  // 10 times
    $obj->set{$fieldName}($item[$fieldName]); // we will run this code 1000 x 10 times, 10k times.
  }
  $objects[]=$obj;
}

Collapse
avinint profile image
Bruno Avinint

Try using PHP 7 if you feel concerned with performance.

Thread Thread
jorgecc profile image
Jorge Castro Author • Edited

PHP 5.x has been discontinued months ago.

Also, opcache is a build-in function.