首页 > 解决方案 > PHP在类if语句中声明方法

问题描述

在 PHP 中,只有当语句为 true 时才可以在类中声明方法:

class MyClass {

    //...
    
   
    if (mode === 'production'):
    public function myMethod() {
        // My cool stuffs here        
    }
    endif;
}

标签: phpif-statementmethods

解决方案


不,你不能,但肯定有一些替代解决方案,比如使用继承。


推荐阅读