Free cookie consent management tool by TermsFeed Generator PHP class | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP class

PHP class

Last Updated on Mar 22, 2023

Introduction

When I want to explain something I normally start small and then explain the harder topics. Today I’m going to do the opposite. I’m going to show you a complete class then we will talk about each part

class Human
{
    private $name;
    private $age;
    private $yearBorn;
    
    public function __construct(string $name, int $yearBorn)
    {
        $this->name = $name;
        $this->yearBorn = $yearBorn;
        $this->calculateAge();
    }
    
    private function calculateAge()
    {
        $this->age = (int) date('Y') - $this->yearBorn;
    }
    
    public function getName()
    {
        return $this->name;
    }
    
    public function getAge()
    {
        return $this->age;
    }
}

$pratham = new Human('pratham', 1997);
echo $pratham->getName().' is '.$pratham->getAge().' years old.';

Class Definition

This is how you define a class.

class ClassName
{

}

Properties

private $name;

This is a variable inside the class. It’s called property. It’s made of 2 parts:

1. access modifier

private: only the class itself can access it ($pratham->name gives an error)

protected: the class itself and it's inherited classes can access it (we'll talk about inheritance in detail later)

public: anyone can access it

2. name of the property

This is the name of the variable and all the rules we mentioned about the name of a variable apply to the properties as well.

Contstructor

__construct()

when you create an object of that class, construct is the function that will be run before anything else .

When we call new Human('pratham', 1997) it’s calling the constructor.

You don’t need to define the __construct() but you can.

Methods

calculateAge , getName, getAge are all functions Functions inside the class are like normal functions. the only difference is that functions inside the class have access modifiers like the properties.

You can read more about functions in another post here.

this Keyword

$this is an important keyword and refers to the current instance (object) that has been made from this class.

For example $this->name in the pratham object is 'pratham' and in amir's instance is 'amir'

->

if you want to call a function or get a variable you should use ->

For example if we call the array like this $example['key'] then if it was an object we would get the value by doing this

$example->key

Or to call a method of that object

$example->methodName()

Class

Now look at it again. You can easily understand what’s happening and what’s going on.

class Human
{
    private $name;
    private $age;
    private $yearBorn;
    
    public function __construct(string $name, int $yearBorn)
    {
        $this->name = $name;
        $this->yearBorn = $yearBorn;
        $this->calculateAge();
    }
    
    private function calculateAge()
    {
        $this->age = (int) date('Y') - $this->yearBorn;
    }
    
    public function getName()
    {
        return $this->name;
    }
    
    public function getAge()
    {
        return $this->age;
    }
}

$pratham = new Human('pratham', 1997);
echo $pratham->getName().' is '.$pratham->getAge().' years old.';

https://youtu.be/AFl6RLu0LIU

Conclusion

Now you know about classes in PHP.

I recommend you to open a PHP files and try writing a class. add methods and properties to it and try to instantiate it.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • define a class
  • properties and methods
  • this keyword
  • ->

Category: programming

Tags: #php #oop

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Arrays
Mar 21, 2023 programming

PHP Arrays

Today we are going to learn about arrays in PHP. Array is a datatype that can hold more than one value so instead of having multiple variables you can hold all those values in one variable. ...

9 Min Read Read More
How to Combine Excel Files Using PHP
May 06, 2024 programming

How to Combine Excel Files Using PHP

Sometimes you might have many excel files that you need to combine, whether it's reports, user data or anything else, merging them into one file with PHP can be easily done. ...

11 Min Read Read More
What Modern PHP Looks Like in 2025
Jul 18, 2025 programming

What Modern PHP Looks Like in 2025

PHP has quietly evolved over the years, shedding many of its dated stereotypes while embracing modern programming practices and tooling. What used to be a language mocked for its inconsistencies and spaghetti-code reputation is now a mature, robust, and highly adaptable part of the web development ecosystem. ...

20 Min Read Read More
Laravel 12 Beginner to Advanced: Complete Guide
Jul 17, 2025 programming

Laravel 12 Beginner to Advanced: Complete Guide

Laravel is a name that resonates throughout the PHP community. With its elegant syntax, robust ecosystem, and developer-friendly tools, Laravel has become the go-to framework for modern web development. Laravel 12, released on February 24, 2025, continues this tradition, streamlining workflows and boosting performance with support for the latest PHP advancements. ...

20 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...