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 Namespace
Mar 23, 2023 programming

PHP Namespace

Today we are going to talk about namespaces in PHP. Namespace is like putting the classes in a group. it helps you organize your code much better. It's a must-know topic for a PHP programmer. ...

7 Min Read Read More
PHP catch exceptions
Feb 14, 2023 programming

PHP catch exceptions

Today we are going to talk about catching exceptions in PHP. Most of the time in your script you might face some exceptions. So today we are going to learn how to catch those exceptions. ...

7 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
A Programmer's Guide to Debugging: Essential Steps to Follow
Mar 23, 2024 programming

A Programmer's Guide to Debugging: Essential Steps to Follow

Debugging is a crucial skill for any programmer, as it helps identify and fix issues in the code. Effective debugging not only improves the overall quality of your software but can also save you time and frustration. ...

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