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

undo git merge the easy way and then undo the undoing! (video)
Nov 27, 2022 programming

undo git merge the easy way and then undo the undoing! (video)

we are going to talk about how to undo git merge and then we talk about how to undo the undoing ...

9 Min Read Read More
PHP array map
Feb 14, 2023 programming

PHP array map

Today we are going to learn about array map function in PHP. It lets you apply a function to the elements of the given arrays and returns an array with changed elements. ...

4 Min Read Read More
PHP array filter
Feb 14, 2023 programming

PHP array filter

Today we are going to talk about array filter in PHP. array filter is a very useful function and it helps you filter an array by key, value or both. ...

6 Min Read Read More
Alpine.js Beginner to Advanced 2025: Everything You Need to Know
Jul 18, 2025 programming

Alpine.js Beginner to Advanced 2025: Everything You Need to Know

JavaScript frameworks have exploded in popularity over the past decade, but not every project needs—or benefits from—the complexity of tools like React or Vue. Sometimes, all you want is to sprinkle interactivity into your HTML without a heavy build process. This is exactly where Alpine.js shines. ...

29 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 ...