[英]Differentiate between methods in a class based on the naming convention?
這是場景:
我有一堂課,沒有預定數量的方法。 一些將是function actionName
,其他將被稱為function filterName
,其中"filter"
和"action"
將定義它們是什么類型的方法, "Name"
可以是任何類型。
在我的__construct()
我想:
$methods = get_class_methods($this);
foreach ( $methods as $method ) {
if ( $method == 'action' )
$this->DoSomething( $this->{$method}() );
if ( $method == 'filter' )
$this->DoSomethingElse( $this->{$method}() );
}
如何不使用任何類型的字符串或reg表達式來實現這一目標?
我也願意使用替代方法get_class_methods()
。 但是請記住,我永遠不會知道有多少種"action"
或"filter"
方法。
if (substr($method,0,6) == "action") { }
if (substr($method,0,6) == "filter") { }
我只是在檢查關鍵字的函數名稱上的前六個字符鏈。 請注意-太多的關鍵字會破壞其性能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.