Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have been writing PHP without "classes" for years... what am I missing?

Tags:

For the life of me, I can't seem to wrap my head around "classes" in PHP.

I have managed to write large, scalable, and popular websites without them.

What am I missing? (And how do I learn?)

like image 758
darkAsPitch Avatar asked Feb 21 '12 09:02

darkAsPitch


People also ask

Why PHP is not fully OOP?

Multiple inheritance is still not there in PHP So it not fully Object Oriented. If the basic language semantics allow for both paradigms, then it's commonly classified as an hybrid language. @Shakti Singh. Multiple inheritance is not a basic concept of OO.

What is the point of classes in coding?

Classes are used to create and manage new objects and support inheritance—a key ingredient in object-oriented programming and a mechanism of reusing code.

Is OOP PHP better than procedural?

Object-oriented programming has several advantages over procedural programming: OOP is faster and easier to execute. OOP provides a clear structure for the programs. OOP helps to keep the PHP code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug.

What is the point of classes and objects?

an object is an element (or instance) of a class; objects have the behaviors of their class. The object is the actual component of programs, while the class specifies how instances are created and how they behave.


2 Answers

Classes will help with code re-use and potentially a very structured application.

Procedural programming can be a lot faster in both development time and execution speed.

OO programming is the more mainstream way but not always the best way. Theres a book called PHP Objects, Patterns and Practice which is a very good read, it covers the basics of classes, why and how to use, abstraction and common design patterns such as MVC. It also covers unit testing and other very good practices for php developers

like image 128
encodes Avatar answered Oct 05 '22 12:10

encodes


The point of classes (object oriented programming) is that it bundles data together with the code that operates on it. If done well, this leads to less tightly coupled and thus more maintainable code.

In practice it means fewer global variables (whether used directly or accessed through static factory methods) and lesss passing around of data (i.e. smaller method signatures).

For a concrete example, look at the Mysqli extension: each function has a procedural and an OOP version, and the procedural version nearly always needs to have an extra "link" parameter to give it context, wheras the OOP version gets that context from the current object.

like image 40
Michael Borgwardt Avatar answered Oct 05 '22 13:10

Michael Borgwardt