Getting All Defined Methods For A Component


I need to be able to tell if a certain method exists for a component/model, whether as a method, inherited, implemented or attached as a behavior.

Think of it as a generalization of PHP’s method_exists().

[size=“2”]I want to be able to provide classes the needed functionality either via an extension of another class, as ‘implementation’ of some interface, or via a behavior - that Yii feature, all of which are already possible, of course. The important note is that I want a unified way (probably via a convenient method) to be able to tell if a certain method exists as a result of either of the 3 ways described. [/size]

[size="2"]I guess something like[font="Arial"] array methodsAvailable(string $method_name) ca[/font]n be a good name for this method.[/size]

[size="2"]Also see a nice suggestion for implementation offered in this forum post (for Yii v1.x).[/size]

[size="2"]What do you think? [/size]

[size=“2”]Does this was already thought of for Yii 2? (I couldn’t find)[/size]

[size="2"]If not, I can submit a PR for this.[/size]



Looks good. But for Yii core components… we would already have this list as a part of the API Documentation and Yii Guides. What are the use cases when developers will need such a function for programming or at runtime?

If we need to check any method for any Yii object, the method_exists function from PHP is anyway available as pointed out. Example:

return method_exists(ActiveRecord::classname(), 'findBySql');

Regarding your question about use cases for this:

There are cases when you code with multiple classes in whatever patterns. Its very useful in some cases to be able to tell at run time if a certain model object has certain capability. By ‘capability’ I mean a method, that can be provided by either standard PHP inheritance or implementation, but also via a Yii attached behavior. The former can be tested with method_exists() but the latter will require an extra call.

I suggested to have a single unified API method that can shorten the code and make it more readable.

This is by no means some ultra important method but it is a nice to have.

Maybe I’ll have time soon to throw it in, in the form of a PR.

Note to self: consider the reply to be mixed: false if does not have, string otherwise, from this closed list:

  • [size=2]‘own’ meaning defined in that class/object and only in it.[/size]
  • [size=2]‘behavior’ meaning this is a Yii behavior.[/size]
  • [size=2]‘inheritance’ a method inherited from super classes[/size]
  • ‘implementation’ same as ‘own’ but was forced to implement it as its an interface implementation
  • more?..