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

Courses