– Beginner to Intermediate Guide
In the previous chapter, we learned how to work with arrays and perform different operations on them. Now, we move one step forward and explore functions and classes, two powerful concepts that help you write clean, reusable, and well-structured PHP code.
Functions and classes are the foundation of professional PHP development. They allow you to organize logic, reduce repetition, and build scalable web applications.
What Are Functions in PHP?
A function is a reusable block of code that performs a specific task. Instead of writing the same logic again and again, you can define it once and call it whenever needed.
Functions can:
- Accept input (parameters)
- Process data
- Return output
Defining a Simple Function
<?php
function helloWorld() {
echo "Hello World";
}
?>
This function is defined but not executed yet. To run it, we must call it.
<?php
helloWorld();
?>
Functions with Parameters
Functions become more useful when they accept parameters.
<?php
function helloUser($name) {
echo "Hello World, " . $name;
}
helloUser("John");
?>
Default Function Parameters
You can also define default values for parameters.
<?php
function displayMessage($message = "World") {
echo "Hello " . $message;
}
displayMessage();
displayMessage("Greg");
?>
Functions That Return Values
Functions are not limited to printing text. They can also return values.
<?php
function addNumbers($a, $b) {
return $a + $b;
}
echo addNumbers(5, 6);
?>
Returned values can also be stored in variables.
<?php
$sum = addNumbers(1, 2);
?>
Practical Example: Discount Calculator Function
<?php
$sweaterPrice = 50;
$percentOff = 0.25;
function couponCode($price, $discount) {
return $price * $discount;
}
echo "The sweater originally costs $" . $sweaterPrice .
" and after discount you pay $" .
($sweaterPrice - couponCode($sweaterPrice, $percentOff));
?>
This example shows how functions help keep calculations clean and reusable.
Introduction to Classes in PHP
A class is a blueprint for creating objects. Classes are part of Object-Oriented Programming (OOP) and allow you to group data (variables) and behavior (functions) together.
Think of a class as a design, and an object as the real instance created from that design.
Creating a Simple Class
<?php
class Student {
}
?>
Class Properties (Member Variables)
<?php
class Student {
public $name;
public $age;
public $major;
}
?>
The public keyword means these properties can be accessed directly.
Creating an Object
<?php
$michael = new Student();
$michael->name = "Michael John";
$michael->age = 27;
$michael->major = "Computer Science";
?>
Constructors in PHP Classes
A constructor is a special function that runs automatically when an object is created.
<?php
class Student {
public $name;
public $age;
public $major;
public function __construct($name, $age, $major) {
$this->name = $name;
$this->age = $age;
$this->major = $major;
}
}
$michael = new Student("Michael John", 27, "Computer Science");
?>
Private Properties and Getter Methods
<?php
class Student {
private $name;
private $age;
private $major;
public function __construct($name, $age, $major) {
$this->name = $name;
$this->age = $age;
$this->major = $major;
}
public function getMajor() {
return $this->major;
}
}
?>
Private properties improve security and control access to data.
Inheritance in PHP
Inheritance allows one class to inherit properties and methods from another.
<?php
class Animal {
public $name;
public $sound;
public function speak() {
echo $this->name . " says " . $this->sound;
}
}
class Dog extends Animal {
public $name = "Dog";
public $sound = "Woof! Woof!";
}
$dog = new Dog();
$dog->speak();
?>
Static Methods in PHP
Static methods can be called without creating an object.
<?php
class Animal {
public static function about() {
echo "This is the animal base class.";
}
}
echo Animal::about();
?>
Conclusion
Functions and classes help you write clean, professional, and scalable PHP applications. Functions reduce repetition, while classes organize data and logic into structured components.
Mastering these concepts prepares you for advanced PHP frameworks like Laravel and real-world backend development.