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

PHP Static

Last Updated on Mar 22, 2023

Introduction

Till now whenever we wanted to call a function inside a class we created a new instance(object) of that class and then called the method. Like this:

Class Human {
    private $name = 'Human';
    public function sayHello(){
        echo 'Hello World! My name is '. $this->name;
    }
}

$human = new Human();
$human->sayHello();

What if we don’t want to create an instance everytime we want to call a function of a class? Then we can use static methods and properties.

Static

Inside the class we define the function as static

public static function sayHello(){
        
}

Then when we want to call it, we use :: rather than -> and there is no need to instantiate the class.

So rather than

$human = new Human();
$human->sayHello();

We write

Human::sayHello();

$this vs self

Inside the static class if you want to get the value of property you can’t use $this keyword anymore. You should use self and rather than -> you should use :: and make sure the property itslef is also static

private static $name = 'Static Human';
public static function sayHello(){
        echo 'Hello World! My name is '. self::$name;
}

So let’s see both of the static and non-static next to each other:

Class Human {
    private $name = 'Human';
    public function sayHello(){
        echo 'Hello World! My name is '. $this->name;
    }
}
$human = new Human();
$human->sayHello();

Class StaticHuman {
    private static $name = 'Static Human';
    public static function sayHello(){
        echo 'Hello World! My name is '. self::$name;
    }
}
StaticHuman::sayHello();

https://youtu.be/1PcMAzliHV0

Conclusion

Now you know about static methods and properties in PHP.

I recommend you to open a PHP files and define static methods and properties for your class and then try to access them.

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

Key takeaways

  • static methods
  • static properties

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 function
Mar 21, 2023 programming

PHP function

Today we are going to learn about functions in PHP. Functions are very important because they can help you divide your application in small bites that can be handled with a function. ...

10 Min Read Read More
PHP Validate URL
Feb 15, 2023 programming

PHP Validate URL

Today we are going to talk about validating emails in PHP. In a lot of projects you need to validate the URLs given by the user to make sure the address has a correct format. ...

4 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
How to Combine Excel Files Using Python
May 08, 2024 programming

How to Combine Excel Files Using Python

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 Python can be easily done. ...

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