4

When I started to learn OOP programming I read that everything is an object. For the most time I develop in PHP. Arrays have important role here. In languages like C# in most cases you really have to use and pass objects not arrays.

For example:

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

    }
}

Is there any sense to create classes for everything when you use PHP? For example:

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

And then instantiate class Product in another class (eg. Model) and return object instead of array (eg. to Controller)?

4 Answers 4

4

The answer is simple.

Everything is an object

is just an ideal.

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

Note that having the powerful array data type is more an advantage than a disadvantage. In my opinion the most lacking OOP feature of PHP is polymorphism, btw.


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

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

Comments

3

No. Not everything needs to be in a class. This isn't Java. :P

PHP is a multi-paradigm language. That means it's perfectly legitimate to use some OO without going whole-hog. The OOP wonks might tell you how horrible a person you are, but OOP is only worthwhile when it reduces overall complexity. Wrapping every value up in a class, for the sake of doing it, adds complexity for no real gain.

A class is basically a template for data that has some unique behavior of its own. If your data doesn't need behavior associated with it (special construction, validation, interpretation, or persistence, for example), wrapping it up in objects is often overkill.

2 Comments

OOP is only worthwhile when it reduces overall complexity ... An OOP-wonk confirms this :) +1 .. Of course there are other reasons to use OOP style beside from complexity. Like flexibility, reusability ...
@hek2mgl: Flexibility and reusability usually do reduce overall complexity, though. :) And where they don't, they can actually be a hindrance. I'm sure nearly all of us have written some stuff with infinite flexibility and reusability in mind, and wound up with a big hairy mess because of it. There's a lot to be said for getting it working now and waiting to add generality til later, when and where you find you need it.
2

In the "everything is an object" mindset, keep in mind this applies to languages that were OO based from day 1. PHP is not one of those languages, objects were added after the fact. So your simple variable types in PHP are not objects, they are smaller & simpler pieces of memory.

It's splitting hairs, but if you don't need to use an object in PHP, then don't, simple arrays are lighter than objects that contain an array. By lighter I mean they use less ram, and in turn, run slightly faster.

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

Comments

2

Answering "Is there any sense to create classes for everything when you use PHP?" as "Is there any sense to create properties in classes for everything when you use PHP?"

Yes, it is very difficult to develop on someone else's code when all that was used was arrays. Specially if there are no constants making it very possible to mistype something to create a bug which would be extremely hard to find.

Also, not that you are not talking about "creating objects for everything" but properties vs a list (array) of values, which is what I based my answer on.

Comments

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.