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

git branch workflow: 7 easy steps (video)
Nov 27, 2022 programming

git branch workflow: 7 easy steps (video)

Transcription Hello This is amir Today we are going to talk about git branching workflow I will go through it step by step and help you understand the process ...

9 Min Read Read More
Top 5 websites to learn PHP for FREE
Dec 22, 2022 programming

Top 5 websites to learn PHP for FREE

Do you want to learn PHP but you don’t want to spend hundreds of dollars? You are not alone. There are many websites that help people like you and me to start our journey. ...

9 Min Read Read More
PHP upload file
Feb 15, 2023 programming

PHP upload file

Today we are going to talk about uploading a file in PHP. Uploading a file is simple but it's very important to know how to handle uploading the files correctly and securely. ...

7 Min Read Read More
What to Do After Learning Programming
Mar 17, 2024 programming

What to Do After Learning Programming

Congratulations! You've learned programming and now have a solid foundation in coding. But what should you do next? ...

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