class ExampleClass { public function exampleMethod() {} protected static function exampleMethod2() {} } $class = new ReflectionClass('ExampleClass'); $modifiers = $class->getMethod('exampleMethod')->getModifiers(); echo $modifiers; // Returns 1 (public) $modifiers2 = $class->getMethod('exampleMethod2')->getModifiers(); echo $modifiers2; // Returns 264 (protected and static)
class ExampleClass { final public function exampleMethod() {} } $class = new ReflectionClass('ExampleClass'); $modifiers = $class->getMethod('exampleMethod')->getModifiers(); if($modifiers & ReflectionMethod::IS_FINAL) { echo 'Method is final'; } else { echo 'Method is not final'; }This example demonstrates how to use the ReflectionClass::getModifiers method to check if a method is marked as final. The method in this example is marked as final, which means it cannot be overridden in a subclass. The returned modifiers value is 17, which is the combination of the MODIFIER_PUBLIC and MODIFIER_FINAL constants. We use the ReflectionMethod::IS_FINAL constant to check if the method is final, by performing a bitwise AND operation with the returned modifiers value. Package/library: This example uses the built-in ReflectionMethod and ReflectionClass classes in PHP, which are part of the core PHP library.