Any benefit to using classes in PHP?

I’m rewriting a PHP site for an auction bidder registration and checkout system. It’s a LAMP system I have used before but like rewriting it so I can take advantage of programming skills learned since last year (usually via work).

One of the pages is for adding and editing Customer information (customers.php) and from I can tell I can perform the actions easily enough in the PHP code before the HTML coding, or I could easily create a class to manage the fields and database activity (get, input, update).

I know PHP is not OO as other languages (Java, C#, etc.). I don’t know if there is a performance benefit, security difference, code location or any other benefit to one way or the other to encapsulate the code in a class.

So is there any benefit to using a class in PHP, or is it “6 to one, half-dozen to another” (or that there is no difference either way)?

On 2015-03-03, dragonbite <dragonbite@no-mx.forums.opensuse.org> wrote:
> One of the pages is for adding and editing Customer information
> (customers.php) and from I can tell I can perform the actions easily
> enough in the PHP code before the HTML coding, or I could easily create
> a class to manage the fields and database activity (get, input,
> update).

Although fluent in many other languages (Python, C++ etc…), I must first admit not being a PHP expert. But from the
nature your question, I don’t think high-level knowledge of PHP is necessary to provide an answer.

> I know PHP is not OO as other languages (Java, C#, etc.).

Like Python, PHP is a general purpose language. It supports classes but you are not compelled to use them.

> I don’t know
> if there is a performance benefit, security difference, code location
> or any other benefit to one way or the other to encapsulate the code in
> a class.

In general, classes do not improve code performance or security. If they did then the Linux kernel writers may have
considered using C++ rather than C (but only after Linus Torvalds was run over by a bus). Classes merely reshape the
organisation of your code in a way that doesn’t necessarily impact on execution. On the other hand there are benefits of
using classes (most notably encouraging code reuse), but these benefits are hotly debated and it’s more a question of
personal coding style rather than anything else. Personally, I favour classes but detest languages that insists on an
everything’s-an-object (a la Java) model since I often like to write some functions outside classes to get the best of
both worlds.

> So is there any benefit to using a class in PHP, or is it “6 to one,
> half-dozen to another” (or that there is no difference either way)?

Obviously it depends on your coding application, coding style, and the experience of your collaborators. If the use of
classes makes you a more productive programmer then by all means use them. But using classes just for the sake of using
them is simply a waste of time.