tags:

views:

298

answers:

8

I have been learning and using PHP for several months now and I am at the stage where I can build almost anything, given the right plans and enough info.

What I'm asking is although I can do just about anything, is it worth learning how to use classes for PHP and using them in place of 'normal' (can't think of the word) code?

From what I've seen scripts made only with classes sacrifice readability, but do they gain any performance increases or anything?

Thanks.

+13  A: 

You mean Object Oriented Programming. Yes, it certainly is worth learning that, and an important step in building good software; if done right, it makes programs more readable and especially, more reusable.

On a side note, though, when asked by programming novices about what language to learn first, I always recommend starting with, or at least also looking at, a strict, strongly typed language like Java, Delphi or C#. There's nothing wrong with PHP (I do almost all my programming in it nowadays and love it), it's just that it's very, very permissive, and forgives a lot of what is considered bad programming style. The tough discipline imposed by a compiler is excellent training to learn clean and structured programming.

Pekka
Agreed – learning OOP can be tricky as it's very easy to misunderstand. I got my first taste of OOP in PHP and got completely the wrong idea for quite a while. It wasn't until I started programming in C# that I really began to understand OOP principles properly.
Will Vousden
+1  A: 

classes introduces the Object Oriented Paradigm to your PHP. Personally, I think that is advantage over coding just a bunch of functions. Because using classes you can create modules and so build your application out of smaller building blocks. Each with its own purpose.

I would say you should go for learning them. There's some good tutorials on it.

Tony
+1  A: 

Script using classes increase readablity, as long as you group together the right code functionalities as classes.

By grouping often used functionalities in classes, you keep details you don't need at the moment out of your way.

It can decrease performance, but just slightly if you keep your code 'lazy'.

Jimmy Shelter
A: 

Yes, it's worth learning. While you can "build almost anything, given the right plans and enough info.", most businesses don't have time to come up with the right plans and enough information, and instead rely on reusing existing code/projects to implement a lot of core functionality. Going forward most of these projects are going to involve some for of classes and Object Oriented Programming.

While you find procedural code more readable, someone trained in a Computer Science program finds class based code more readable. So yes, it's worth learning, because you'll need to understand other people's code and they'll be using classes.

Alan Storm
+8  A: 

Yes yes yes and yes.

You say that structuring your code into classes sacrifices readability but actually once you are used to it, it makes things much clearer.

Not only that, good code structure can make seemingly difficult tasks an absolute piece of cake and what may appear at first to be complex logic becomes pretty trivial when you have things organised. Most methods I add to classes are just a few lines long and really simple but when you put them all together you end up with very powerful and flexible code

And if that's not enough, the potential for code reuse is much higher (i think anyway) in a well-structed object-oriented environment which is always a bonus

I could carry on ranting about the benefits of OOP over procedural code all day but suffice to say that anyone I know who has started using OOP in their PHP code has stuck with it.

Do it man, it's so worth it!!

Addsy
Accepted your answer because you said the word that slipped my mind, not to mention a good answer. :) Looks like I'm off to learn classes. Cheers.
Matt
A: 

In short: Definitley.

Not only will you benefit from reusuale code, less repetition and others mentioned but understanding the principles of OOP will help you structure your programs aswell.

cast01
A: 

In addition to what everybody else already have said, object oriented principles also often make it easier to troubleshoot and maintain code. This is because you group all functionality to distinct, self-contained classes. If you have a trouble with something, you can pin down which object the problem is related to and (often) only conentrate on that particular class of functions. You can also extend the code in the future, both by inheriting from earlier work (creating a new class that implements or extends the old one) and by changing inside a function as long as that function still takes the same parameters and returns the same values.

Some of these advantages are not strictly bound to object oriented programming, but the OOP principles greatly improves this both in thinking and in implementation.

  • Easy to extend
  • Easy to troubleshoot
  • Easy to maintain
Emil Vikström
A: 

PHP Classes are very useful to learn because usually when you find PHP code online it could be written in classes. So I presume it is a good idea to learn PHP classes because it will help with analysing and editing other peoples code.

That said I must go and learn Object Orientated Programming in PHP myself...

Chaim Chaikin