In this section, we will delve into the core concepts of Object-Oriented Programming (OOP) in PHP: properties and methods. Understanding these concepts is crucial for building robust and maintainable PHP applications.

What are Properties and Methods?

Properties

Properties are variables that belong to a class. They are used to store data related to objects created from the class.

Methods

Methods are functions that belong to a class. They define the behavior of the objects created from the class.

Defining Properties and Methods

Defining Properties

Properties are defined within a class using the public, protected, or private access modifiers.

class Car {
    public $color; // Public property
    private $engine; // Private property
    protected $fuel; // Protected property
}

Defining Methods

Methods are defined similarly to functions but within a class context.

class Car {
    public $color;
    private $engine;
    protected $fuel;

    // Public method
    public function startEngine() {
        echo "Engine started";
    }

    // Private method
    private function checkFuel() {
        echo "Checking fuel level";
    }

    // Protected method
    protected function refuel() {
        echo "Refueling";
    }
}

Accessing Properties and Methods

Creating an Object

To access properties and methods, you first need to create an object of the class.

$myCar = new Car();

Accessing Public Properties and Methods

Public properties and methods can be accessed directly using the object.

$myCar->color = "Red"; // Setting a public property
echo $myCar->color; // Getting a public property

$myCar->startEngine(); // Calling a public method

Accessing Private and Protected Properties and Methods

Private and protected properties and methods cannot be accessed directly from outside the class. They can only be accessed within the class itself or by derived classes (in the case of protected members).

class Car {
    private $engine;

    public function setEngine($engineType) {
        $this->engine = $engineType; // Accessing private property within the class
    }

    public function getEngine() {
        return $this->engine; // Accessing private property within the class
    }
}

$myCar = new Car();
$myCar->setEngine("V8");
echo $myCar->getEngine(); // Outputs: V8

Practical Example

Let's create a more comprehensive example to illustrate properties and methods in action.

class Car {
    public $color;
    private $engine;
    protected $fuel;

    public function __construct($color, $engine, $fuel) {
        $this->color = $color;
        $this->engine = $engine;
        $this->fuel = $fuel;
    }

    public function startEngine() {
        if ($this->checkFuel()) {
            echo "Engine started";
        } else {
            echo "Not enough fuel";
        }
    }

    private function checkFuel() {
        return $this->fuel > 0;
    }

    protected function refuel($amount) {
        $this->fuel += $amount;
    }
}

$myCar = new Car("Red", "V8", 10);
echo $myCar->color; // Outputs: Red
$myCar->startEngine(); // Outputs: Engine started

Exercises

Exercise 1: Define a Class with Properties and Methods

  1. Define a class Person with the following properties:

    • name (public)
    • age (private)
    • gender (protected)
  2. Add a public method introduce that prints a message introducing the person.

  3. Add a private method isAdult that returns true if the person's age is 18 or above.

  4. Create an object of the Person class and call the introduce method.

Solution

class Person {
    public $name;
    private $age;
    protected $gender;

    public function __construct($name, $age, $gender) {
        $this->name = $name;
        $this->age = $age;
        $this->gender = $gender;
    }

    public function introduce() {
        echo "Hi, my name is {$this->name}.";
        if ($this->isAdult()) {
            echo " I am an adult.";
        } else {
            echo " I am not an adult.";
        }
    }

    private function isAdult() {
        return $this->age >= 18;
    }
}

$person = new Person("John", 20, "Male");
$person->introduce(); // Outputs: Hi, my name is John. I am an adult.

Summary

In this section, we covered:

  • The definition and purpose of properties and methods in PHP classes.
  • How to define and access public, private, and protected properties and methods.
  • Practical examples to illustrate the concepts.
  • An exercise to reinforce the learned concepts.

Understanding properties and methods is fundamental to mastering OOP in PHP. In the next section, we will explore inheritance, which allows classes to inherit properties and methods from other classes, promoting code reuse and organization.

PHP Programming Course

Module 1: Introduction to PHP

Module 2: Control Structures

Module 3: Functions

Module 4: Arrays

Module 5: Working with Forms

Module 6: Working with Files

Module 7: Object-Oriented Programming (OOP)

Module 8: Working with Databases

Module 9: Advanced PHP Techniques

Module 10: PHP Frameworks and Best Practices

Module 11: Project: Building a Web Application

© Copyright 2024. All rights reserved