HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP Classes and Objects

Classes and Objects are the two main aspects of object-oriented programming. A **Class** acts as a blueprint or a template, while an **Object** is an individual instance created from that blueprint.


1. What is a Class?

A class is a user-defined data type. It defines the structure of the data and the functions that can operate on that data. Think of a class as a blueprint for a house. It shows where the walls, doors, and windows should be, but it isn't a house itself.

Defining a Class

You define a class using the class keyword followed by the name of the class.

<?php
    class Fruit {
        // Properties and methods go here
    }
?>
Naming Convention: By convention, class names in PHP usually start with an Uppercase letter and use PascalCase (e.g., UserAccount, ProductManager).

2. Properties and Methods

Inside a class, you define variables (called **Properties**) and functions (called **Methods**).

  • Properties: Variables that hold data about the object.
  • Methods: Functions that define what the object can do.
<?php
    class Fruit {
        // Properties
        public $name;
        public $color;

        // Methods
        function set_name($name) {
            $this->name = $name;
        }
        function get_name() {
            return $this->name;
        }
    }
?>
The $this Keyword: The $this keyword refers to the **current instance** of the class. It is used to access properties or methods within the class definition.

3. What is an Object?

An object is an instance of a class. If the class is the blueprint, the object is the actual house built from that blueprint. You can create many objects from a single class, and each object can have its own data.

Instantiating an Object

You create an object using the new keyword.

<?php
    // Instantiate the object
    $apple = new Fruit();
    $banana = new Fruit();

    // Access properties and methods using ->
    $apple->set_name('Apple');
    $banana->set_name('Banana');

    echo $apple->get_name(); // Outputs: Apple
    echo $banana->get_name(); // Outputs: Banana
?>

Key Differences

Feature Class Object
Analogy A Blueprint / Template. A Physical Building / Instance.
Memory Does not occupy memory space. Occupies memory space when created.
Existence Defined once in your code. Multiple instances can exist at once.

Using the instanceOf Keyword

You can check if an object belongs to a specific class using the instanceof keyword. This is very useful for validation.

<?php
    $apple = new Fruit();
    var_dump($apple instanceof Fruit); // Outputs: bool(true)
?>
Pro Tip: Always initialize your objects properly. In the next lesson, we will see how **Constructors** can help you set property values as soon as an object is created.

Summary

  • A Class is a template for objects.
  • An Object is a specific instance of a class.
  • Use the class keyword to define a class.
  • Use the new keyword to create an object.
  • Use -> (arrow operator) to access properties and methods of an object.