WikiGalaxy

Personalize

PHP Classes and Objects

Introduction to PHP Classes

In PHP, a class is a blueprint for creating objects. It defines properties and methods that the created objects can use.

Defining a Class

A class is defined using the <class> keyword followed by the class name and a pair of curly braces.


class Car {
    public $color;
    public $model;
    public function __construct($color, $model) {
        $this->color = $color;
        $this->model = $model;
    }
    public function message() {
        return "My car is a " . $this->color . " " . $this->model . "!";
    }
}
    

Creating an Object

An object is created using the <new> keyword followed by the class name.


$myCar = new Car("black", "Volvo");
echo $myCar->message();
    

Console Output:

My car is a black Volvo!

PHP Class Properties

Understanding Properties

Properties are variables that belong to a class. They are defined within the class and can have different visibility levels.

Access Modifiers

PHP supports three types of access modifiers: public, protected, and private.


class Fruit {
    public $name;
    protected $color;
    private $weight;
}
    

Public Properties

Public properties can be accessed from anywhere, both inside and outside the class.


$apple = new Fruit();
$apple->name = "Apple"; // OK
    

Console Output:

Apple

PHP Class Methods

Defining Methods

Methods are functions that are defined inside a class. They define the behavior of the class objects.

Method Example

Define a method within a class to perform specific operations or actions.


class Dog {
    public $name;
    public function bark() {
        return "Woof! Woof!";
    }
}
    

Calling Methods

Methods are called using the -> operator on the object.


$dog = new Dog();
echo $dog->bark(); // Outputs: Woof! Woof!
    

Console Output:

Woof! Woof!

PHP Constructors

Role of Constructors

Constructors are special methods that are automatically called when an object is created. They initialize object properties.

Constructor Syntax

Constructors in PHP are defined using the __construct() method.


class Cat {
    public $name;
    public function __construct($name) {
        $this->name = $name;
    }
}
    

Creating Objects with Constructors

When creating an object, parameters can be passed to the constructor to set initial values.


$cat = new Cat("Whiskers");
echo $cat->name; // Outputs: Whiskers
    

Console Output:

Whiskers

PHP Inheritance

Concept of Inheritance

Inheritance allows a class to inherit properties and methods from another class. This promotes code reuse.

Base and Derived Classes

A class that is inherited is called a base class, and the class that inherits is called a derived class.


class Animal {
    public function makeSound() {
        return "Some sound";
    }
}
class Dog extends Animal {
    public function makeSound() {
        return "Bark";
    }
}
    

Overriding Methods

Derived classes can override methods from the base class to provide specific implementations.


$dog = new Dog();
echo $dog->makeSound(); // Outputs: Bark
    

Console Output:

Bark

PHP Static Methods and Properties

Static Keyword

Static properties and methods belong to the class rather than any object instance. They are accessed using the scope resolution operator ::.

Declaring Static Members

Static members are declared using the static keyword.


class MathOperations {
    public static $pi = 3.14;
    public static function square($x) {
        return $x * $x;
    }
}
    

Accessing Static Members

Static members can be accessed without creating an object of the class.


echo MathOperations::$pi; // Outputs: 3.14
echo MathOperations::square(5); // Outputs: 25
    

Console Output:

3.14

25

PHP Abstract Classes

Purpose of Abstract Classes

Abstract classes cannot be instantiated directly. They are designed to be extended by other classes.

Abstract Methods

Abstract methods are declared in an abstract class and must be defined in derived classes.


abstract class Vehicle {
    abstract public function startEngine();
}
class Car extends Vehicle {
    public function startEngine() {
        return "Engine started";
    }
}
    

Implementing Abstract Methods

Derived classes must implement all abstract methods from their parent abstract class.


$car = new Car();
echo $car->startEngine(); // Outputs: Engine started
    

Console Output:

Engine started

PHP Interfaces

Understanding Interfaces

Interfaces allow you to specify what methods a class should implement. They provide a way to enforce certain behaviors across classes.

Defining an Interface

Interfaces are defined using the interface keyword.


interface Animal {
    public function makeSound();
}
class Cat implements Animal {
    public function makeSound() {
        return "Meow";
    }
}
    

Implementing Interfaces

Classes implementing an interface must define all its methods.


$cat = new Cat();
echo $cat->makeSound(); // Outputs: Meow
    

Console Output:

Meow

PHP Traits

Purpose of Traits

Traits are a mechanism for code reuse in single inheritance languages like PHP. They allow you to include methods in multiple classes.

Defining a Trait

Traits are defined using the trait keyword.


trait Logger {
    public function log($message) {
        echo $message;
    }
}
class Application {
    use Logger;
}
    

Using Traits in Classes

Traits are included in classes using the use keyword.


$app = new Application();
$app->log("Application started"); // Outputs: Application started
    

Console Output:

Application started

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025