How to call to an static class and method with php?

by dalton_moen , in category: PHP General , 5 months ago

How to call to an static class and method with php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , a month ago

@dalton_moen 

To call a static class and method in PHP, you can use the scope resolution operator (::). Here's an example:

1
2
3
4
5
6
7
8
class MyClass {
    public static function myStaticMethod() {
        return "Hello, World!";
    }
}

$result = MyClass::myStaticMethod();
echo $result;


In this example, we have a static method myStaticMethod within the MyClass class. We call the method using the scope resolution operator :: followed by the method name.


The result of calling the static method is then stored in a variable and printed out using echo.