Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all method names starting with a substring from a PHP object

I have an object and want a method that returns how much method this Object have that start with bla_.

I found get_class_methods() which returns all method names, but I only want names which starts with bla_

like image 338
gustavgans Avatar asked Dec 01 '09 14:12

gustavgans


People also ask

What does :: class do in PHP?

SomeClass::class will return the fully qualified name of SomeClass including the namespace. This feature was implemented in PHP 5.5. It's very useful for 2 reasons. You can use the use keyword to resolve your class and you don't need to write the full class name.

What is invoke method in PHP?

The ReflectionMethod::invoke() function is an inbuilt function in PHP which is used to invoke the specified reflected method and returns the result of the method. Syntax: public mixed ReflectionMethod::invoke ( $object, $parameter )

How can I access a method from another class in PHP?

Using singleton in PHP Another way to access methods in another class, is to use singleton. This makes it possible to use methods without the need to first instantiate the class. The object is instead created inside the class itself.


1 Answers

You can use preg_grep() to filter them:

$method_names = preg_grep('/^bla_/', get_class_methods($object)); 
like image 195
soulmerge Avatar answered Sep 30 '22 18:09

soulmerge