Kirill Fuchs
Kirill Fuchs

Reputation: 13686

Differentiate between methods in a class based on the naming convention?

Here is the scenario:

I have a class with no predetermined amount of methods. Some will be function actionName and others will be called function filterName where "filter" and "action" will define what type of method they are and "Name" can be anything.

In my __construct() I want to:

$methods = get_class_methods($this);

foreach ( $methods as $method ) {

  if ( $method == 'action' )
    $this->DoSomething( $this->{$method}() );

  if ( $method == 'filter' )
    $this->DoSomethingElse( $this->{$method}() );

}

How can I achieve this without using any type of string or reg expressions?

I am also open to using an alternative to get_class_methods(). But remember I will never know how many "action" or "filter" methods exists.

Upvotes: 1

Views: 56

Answers (1)

Sébastien Renauld
Sébastien Renauld

Reputation: 19662

if (substr($method,0,6) == "action") { }

if (substr($method,0,6) == "filter") { }

I'm simply checking the first six character-chain on the name of the function for the keywords. Be warned - too many keywords will kill the performance of this.

Upvotes: 1

Related Questions