Pregunta

Is it possible in PHP to call all functions that have a certain prefix ?

for example i have 70 functions most start with

func1.... func2 ....

i want to call all functions that start with _func1 is there any PHP function for that ? something like call_user_func but instead of a callback name to pass it something like '_func1*'

i know i can do it with 'get_defined_functions' but wonder if there's already such a function.

¿Fue útil?

Solución

$names=get_defined_functions();
foreach($names["user"] as $f)
{
  if(substr($f,0,6)=="_func1")
   {
       // now call $f
      //  call_user_func($f);
   }

}

Otros consejos

In case you are using methods from class, you can also use following :

for ex :

class myclass {
    // constructor
    function myclass()
    {
        return(true);
    }

    // method 1
    function myfunc1()
    {
        return(true);
    }

    // method 2
    function myfunc2()
    {
        return(true);
    }
}

$class_methods = get_class_methods('myclass');
// or
$class_methods = get_class_methods(new myclass());

foreach ($class_methods as $method_name) {
    echo "$method_name\n";
}

you can use sub string method in loop to get it working as per your need

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top