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

PHP Variables

Today we are going to learn about variables in PHP. Variables are very important and powerful in any programming language. So if you are learning PHP it's very important to know about variables. ...

5 Min Read Read More
PHP Loops
Mar 21, 2023 programming

PHP Loops

Today we are going to learn about loops in PHP. Loop is another important concept in programming and we are going to learn everything about loops in PHP. ...

11 Min Read Read More
PHP Exec
Mar 22, 2023 programming

PHP Exec

Today we are going to talk about exec in PHP. Did you know we can run system commands through PHP? Well not only it’s possible but also it’s very easy. You can do it with exec function ...

3 Min Read Read More
PHP Simple Web Crawler
Mar 22, 2023 programming

PHP Simple Web Crawler

Today we are going to build a simple web crawler in PHP and parse the box office movies from IMDB. We can do it with PHP easily. We don’t even need any extra package. ...

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