Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a method is static in PHP?

I need to know whether the method is declared as static given its name and the name of the class containing it. method_exists provides true for both static and non-static methods.

like image 677
Fluffy Avatar asked Sep 22 '11 10:09

Fluffy


People also ask

How do you know if a method should be static?

You should consider making a method static in Java : 1) If a method doesn't modify the state of the object, or not using any instance variables. 2) You want to call the method without creating an instance of that class.

What is a static method PHP?

Definition and Usage. The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.

How do you call a non-static method from a static method in PHP?

In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. See Static methods (php.net) for details. In the following example, the method foo() is called as dynamic while actually it is static.

When use static methods PHP?

When you use static, this is to call a function without an instance of the class. The main reason is often to represent a service class which should not be instantiated many times. A static class (with only static functions) prevent you from using many OOP features like inheritance, interface implementation.


1 Answers

Here's a little more clear way on how to use ReflectionMethod:

$MethodChecker = new ReflectionMethod($ClassName,$MethodName);
var_dump($MethodChecker->isStatic());
like image 53
Nathan J.B. Avatar answered Sep 22 '22 06:09

Nathan J.B.