What Are Classes and Objects in PHP

🔷 What Are Classes and Objects in PHP?

Class → A blueprint or template

Object → A real instance created from a class

Real-life example:

  • Class = “Car blueprint”

  • Object = “Red BMW car”, “Black Audi car”


🟦 1. Creating Your First Class in PHP


 

This only defines a blueprint; no real object exists yet.


🟩 2. Adding Properties (Variables inside class)



 


Properties = data of the object.


🟧 3. Adding Methods (Functions inside class)


 

Method = action that object can do.


🟥 4. Creating Objects from a Class


 


🟪 5. Creating Multiple Objects


 

Each object has its own data.


🟨 6. Constructor Method (Automatically Runs When Object is Created)

__construct() runs automatically when you create an object.


 


🟫 7. $this Keyword (Very Important)

$this refers to current object.

Example:



 

Means…
Set object’s color = value coming from constructor.


🟦 8. Full Simple Example



 


🎉 Summary (Easy Table)

Term Meaning
Class Blueprint/template
Object Real entity created from class
Property Variables inside class
Method Functions inside class
Constructor Auto-run method
$this Refers to current object

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *