PHP using objects instead of arrays

When I started learning OOP programming, I read that this is all an object. In most cases, I develop in PHP. Arrays play an important role here. In languages ​​like C #, in most cases you really need to use and pass objects, not arrays.

For instance:

Class Product { private $data = array(); public function __construct() { $this->data['setting_1'] = 'a'; $this->data['setting_2'] = 'b'; $this->data['setting_3'] = 'c'; $this->data['setting_4'] = 'd'; $this->data['setting_5'] = 'e'; } } 

Does it make sense to create classes for everything when you use PHP? For instance:

 Class Product { private $setting_1, $setting_2, $setting_3, $setting_4, $setting_5; } 

And then create an instance of the Product class in another class (for example, Model) and return the object instead of an array (for example, in the controller)?

+4
source share
4 answers

In thinking "everything is an object", keep in mind that this applies to languages ​​that have been based on OO since day one. PHP is not one of these languages, after adding objects, objects were added. Thus, your simple types of variables in PHP are not objects, they are smaller and simpler pieces of memory.

It splits hair, but if you do not need to use an object in PHP, then no, simple arrays are lighter than objects containing an array. By lighter, I mean that they use less bar and, in turn, work a little faster.

The best advice you can get about writing PHP is KISS (Keep It Simple Stupid).

+2
source

The answer is simple.

All this object

is just an ideal.

In most real-world OOP languages, simple data types exist. Even in Java or CSharp. In PHP, there is even an array as a complex, non-object data type. You should not worry about using this, also in the context of OOP, of course.

Please note that having a powerful dataset has more advantages than a disadvantage. In my opinion, the most missing feature of OOP for PHP is polymorphism, by the way.


However, PHP5 introduced a set of iterators and data structures , in the so-called Standard PHP Library (SPL) extension, which is part of the main PHP distribution. You can look at them, but in most cases the data type of the array should work well (and execute).

+4
source

The answer is "Does it make sense to create classes for everything when you use PHP?" like "Does it make sense to create properties in classes for everything when you use PHP?"

Yes, it is very difficult to develop another user's code when everything that was used was an array. Specifically, if there are no constants that allow you to do something wrong in order to create an error that would be very difficult to find.

In addition, it’s not that you’re not talking about “creating objects for everything”, but about properties against a list (array) of values, which I based my answer on.

+2
source

No. Not everything should be in class. This is not Java .: P

PHP is a language with several paradigms. This means that it is completely legal to use some kind of OO without kissing the whole thing. PLO winners can tell you what horror you are, but PLO only costs when it reduces overall complexity. Dipping each value in the class, for the sake of this, adds complexity without real gain.

A class is basically a data template that has its own unique behavior. If your data does not require the behavior associated with it (for example, special construction, validation, interpretation, or persistence), wrapping them in objects is often unnecessary.

+2
source

Source: https://habr.com/ru/post/1487347/


All Articles