This is a very useful, step-by-step, and long tutorial for all the basics about PHP class and object. Stick with it, manually follow the tutorial and recreate the examples, so that you actually memorize the concepts. It's worht the effort!
The topics include:
- Understanding Object-Oriented Programming
- Understanding Objects and Classes
- Recognizing the Differences Between Objects and Classes
- Structuring Classes Defining Class Properties
- Defining Class Methods
- Magic Methods in OOP
- Using Constructors and Destructors
- Converting to a String
- Using Class Inheritance
- Overwriting Inherited Properties and Methods
- Preserving Original Method Functionality While Overwriting Methods
- Assigning the Visibility of Properties and Methods
- Public Properties and Methods
- Protected Properties and Methods
- Private Properties and Methods
- Static Properties and Methods Commenting with DocBlocks
- Comparing Object-Oriented and Procedural Code
- Reason 1: Ease of Implementation
- The Procedural Approach
- The OOP Approach
- Reason 2: Better Organization
- Reason 3: Easier Maintenance
A class, for example, is like a blueprint for a house. It defines the shape of the house on paper, with relationships between the different parts of the house clearly defined and planned out, even though the house doesn't exist.
An object, then, is like the actual house built according to that blueprint. The data stored in the object is like the wood, wires, and concrete that compose the house: without being assembled according to the blueprint, it's just a pile of stuff. However, when it all comes together, it becomes an organized, useful house.
As you can see, OOP keeps objects as separate entities, which makes for easy separation of different pieces of code into small, related bundles.