php判断继承类有没有对指定方法重写


php判断继承类有没有对指定方法重写,在被继承的类实例化时加入以下代码:



  
  
   use ReflectionClass;
   
    public function __construct()
    {
    
        $reflectClass             = new ReflectionClass($this);
        
        //必须重写的方法
        $requiredOverwriteMethods = [
            'isCurrentLimit',
            'genCacheKey',
            'really',
        ];
        
        foreach ($requiredOverwriteMethods as $method) {
            if ($reflectClass->getMethod($method)->getDeclaringClass()->getName() !== $reflectClass->getName()) {
                throw new \Exception('继承:'.__CLASS__.'的子类必须重写方法:'.$method);
            }
        }
        unset($method);
        
    }


鼎云博客
  • 最新评论
  • 总共0条评论