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

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
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 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
What Modern PHP Looks Like in 2025
Jul 18, 2025 programming

What Modern PHP Looks Like in 2025

PHP has quietly evolved over the years, shedding many of its dated stereotypes while embracing modern programming practices and tooling. What used to be a language mocked for its inconsistencies and spaghetti-code reputation is now a mature, robust, and highly adaptable part of the web development ecosystem. ...

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