What are Classes and Objects?
-
A class is like a blueprint for creating objects.
-
An object is an instance of a class — it has properties (data) and methods (functions).
Defining a Class
Creating Objects
Constructor Method
Use a constructor to initialize properties when the object is created.
Object-Oriented Concepts
| Concept | Description |
|---|---|
| Class | Blueprint or template |
| Object | Instance of a class |
| Property | Variable inside a class |
| Method | Function inside a class |
| Constructor | Special method that runs when object is created |
| $this | Refers to the current object |
Visibility (Access Modifiers)
| Modifier | Meaning |
|---|---|
public |
Accessible from anywhere |
private |
Only accessible within the class |
protected |
Accessible within class & subclasses |
Summary
-
Use classes to organize related code.
-
Create objects from classes to work with that code.
-
Use properties for data and methods for behavior.
-
Encapsulation helps protect and manage data (using
private/public).
