Classes and Objects

Introduction

Classes and Objects are fundamental concepts in Object-Oriented Programming (OOP) in PHP. They help developers organize code into reusable structures, making applications easier to manage, maintain, and scale.

A class acts as a blueprint, while an object is an instance of that class.

Objectives

By the end of this training, you will be able to:

  • Understand classes and objects in PHP
  • Create classes using PHP syntax
  • Define properties and methods
  • Create and use objects
  • Access class members
  • Understand constructors
  • Build reusable and organized code

What is a Class

A class is a template or blueprint used to create objects. It contains variables and functions that define the behavior of the object.

Example of a Class

<?php
class Student {
public $name;
public $age;
}
?>

In this example:

  • Student is the class name
  • $name and $age are properties

What is an Object

An object is an instance of a class. Objects use the structure defined in the class.

Creating an Object

<?php
class Student {
public $name;
public $age;
}

$student1 = new Student();
?>

Here:

  • new Student() creates an object
  • $student1 is the object variable

Accessing Properties

You can assign and display values using the object operator.

<?php
class Student {
public $name;
public $age;
}

$student1 = new Student();

$student1->name = "Ali";
$student1->age = 20;

echo $student1->name;
echo $student1->age;
?>

Methods in a Class

Methods are functions inside a class.

Example of a Method

<?php
class Student {

public function message() {
echo "Welcome to PHP OOP";
}
}

$student1 = new Student();
$student1->message();
?>

Using Properties and Methods Together

<?php
class Car {

public $brand;

public function showBrand() {
echo $this->brand;
}
}

$car1 = new Car();

$car1->brand = "Toyota";
$car1->showBrand();
?>

Understanding the $this Keyword

The $this keyword refers to the current object inside the class.

It is used to access properties and methods within the same class.

Example

<?php
class Employee {

public $name;

public function setName($name) {
$this->name = $name;
}

public function getName() {
return $this->name;
}
}

$emp = new Employee();

$emp->setName("Ahmed");

echo $emp->getName();
?>

Constructors in PHP

A constructor is a special method that automatically runs when an object is created.

Constructor Example

<?php
class Mobile {

public $brand;

public function __construct($brand) {
$this->brand = $brand;
}

public function display() {
echo $this->brand;
}
}

$phone = new Mobile("Samsung");
$phone->display();
?>

Advantages of Classes and Objects

  • Organizes code efficiently
  • Promotes code reusability
  • Simplifies large projects
  • Improves maintenance
  • Supports real-world modeling
  • Makes applications scalable

Real World Examples of OOP

Classes and objects are used in:

  • Student Management Systems
  • E-commerce Websites
  • Banking Applications
  • Hospital Management Systems
  • Content Management Systems
  • Online Booking Platforms

Best Practices

  • Use meaningful class names
  • Keep methods simple and focused
  • Reuse code whenever possible
  • Use constructors for initialization
  • Follow object-oriented principles

Summary

Classes and Objects are essential concepts in PHP Object-Oriented Programming. Classes define the structure, while objects bring that structure to life. Understanding OOP helps developers build professional, maintainable, and scalable web applications.

Home Ā» Advanced PHP > Object-Oriented PHP > Classes and Objects