re: PHP is bad for Object-Oriented Programming OOP VIEW POST

FULL DISCUSSION
 

Your examples are straight from the 90s Jorge.
PHP has evolved a lot since then I think you need to freshen up your knowledge when it comes to PHP as a language.

  • 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.

  • If you need custom JSON serialisation logic just implement the JsonSerializable interface, I mean it is available since PHP 5.4 and the fact that you are not aware of it just shows that you haven't worked that much with PHP

  • You are instantiating dependency objects in the constructor instead of type hinting and passing them through the constructor your example should've looked like this:

class Customer {

    public $category;

    public function __construct(Category $category) 
    {
          $this->category = $category;
    }
}

This renders your point moot, you wouldn't event be able to instantiate the object without Category, let alone do what your example shows.

You are mixing apples and oranges, a strong type system has nothing to do with OOP, perfect example is the Ruby language, it is an Object Oriented language with dynamic type system.

Most of the languages support multi-paradigm programming nowadays anyways. I'm just saddened by this article because it's full of wrong info that could mislead junior developers.

 

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;
}

code of conduct - report abuse