codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

44 / 56 topics
43Object-Oriented Programming in PHP44Classes and Objects45Inheritance in PHP46Polymorphism in PHP47Interfaces in PHP48Traits in PHP
Tutorials/PHP/Classes and Objects
🐘PHP

Classes and Objects

Updated 2026-05-15
10 min read

Classes and Objects

Introduction

In PHP, Object-Oriented Programming (OOP) is a popular paradigm for structuring software. At the core of OOP are classes and objects. A class is a blueprint for creating objects, providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). An object is an instance of a class.

Understanding how to define and use classes and objects is fundamental to leveraging PHP's OOP capabilities effectively. This tutorial will guide you through the basics of defining classes and creating objects in PHP, along with practical examples.

Concept

Classes

A class in PHP is defined using the class keyword followed by the class name. Inside a class, you can define properties (variables) and methods (functions). Properties represent the state of an object, while methods define the behavior or actions that objects can perform.

Defining a Class

Here's a simple example of defining a class in PHP:

php
1<?php
2class Car {
3 // Property declaration
4 public $color = 'red';
5
6 // Method declaration
7 function drive() {
8 echo "The car is driving.";
9 }
10}
11?>

In this example, the Car class has a property $color initialized to 'red', and a method drive() that outputs a message.

Objects

An object is an instance of a class. You create objects using the new keyword followed by the class name. Once created, you can access the properties and methods of the object using the arrow operator (->).

Creating and Using Objects

Here's how you can create an object from the Car class and use its properties and methods:

php
1<?php
2// Create a new Car object
3$myCar = new Car();
4
5// Accessing the property
6echo $myCar->color; // Outputs: red
7
8// Calling the method
9$myCar->drive(); // Outputs: The car is driving.
10?>

In this example, $myCar is an instance of the Car class. We access its color property and call its drive() method.

Examples

Example 1: Basic Class with Constructor

A constructor is a special method that is automatically called when an object is created. In PHP, constructors are named __construct(). Here's an example:

php
1<?php
2class Person {
3 public $name;
4 public $age;
5
6 // Constructor
7 function __construct($name, $age) {
8 $this->name = $name;
9 $this->age = $age;
10 }
11
12 function introduce() {
13 echo "Hello, my name is {$this->name} and I am {$this->age} years old.";
14 }
15}
16
17// Create a new Person object
18$person1 = new Person('Alice', 30);
19$person1->introduce(); // Outputs: Hello, my name is Alice and I am 30 years old.
20?>

In this example, the Person class has a constructor that initializes the name and age properties. The introduce() method outputs a greeting with the person's name and age.

Example 2: Access Modifiers

PHP supports access modifiers to control the visibility of properties and methods. The three main access modifiers are:

  • public: Accessible from anywhere.
  • protected: Accessible within the class and by inheriting classes.
  • private: Accessible only within the class.

Here's an example demonstrating access modifiers:

php
1<?php
2class Animal {
3 public $name;
4 protected $type;
5 private $age;
6
7 function __construct($name, $type, $age) {
8 $this->name = $name;
9 $this->type = $type;
10 $this->age = $age;
11 }
12
13 function displayInfo() {
14 echo "Name: {$this->name}, Type: {$this->type}, Age: {$this->age}";
15 }
16}
17
18$animal = new Animal('Lion', 'Mammal', 5);
19echo $animal->name; // Outputs: Lion
20// echo $animal->type; // Error: Cannot access protected property
21// echo $animal->age; // Error: Cannot access private property
22?>

In this example, the Animal class has properties with different access modifiers. The displayInfo() method can access all properties because it is within the same class.

What's Next?

Now that you have a solid understanding of classes and objects in PHP, the next step is to explore inheritance. Inheritance allows one class to inherit properties and methods from another class, promoting code reusability and creating a hierarchical relationship between classes.

Stay tuned for more tutorials on Object-Oriented Programming in PHP!

Info

Remember, practice makes perfect! Try creating your own classes and objects with different properties and methods to reinforce what you've learned.


PreviousObject-Oriented Programming in PHPNext Inheritance in PHP

Recommended Gear

Object-Oriented Programming in PHPInheritance in PHP