How to load php code dynamically and check if classes implement interface

天大地大妈咪最大 提交于 2020-01-24 09:16:07

问题


I'm loading a class dynamically in PHP. This file and class name are gotten out of the database. This file must contain a class and a method. I tried to solve it with an interface, but I don't really get it how I could do it the most beautiful way.

What would be your suggestions?


回答1:


Use class_exists() to determine if a class has been defined, method_exists() to determine if a class has a method and instanceof to determine if a class implements an interface.




回答2:


To check whether a class has been defined, use:

   if (class_exists('ClassName')) {
      // Do something
   }

To check whether a method/function exists, use:

   if (method_exists('methodName')) {
      // Do something
   }


来源:https://stackoverflow.com/questions/9604090/how-to-load-php-code-dynamically-and-check-if-classes-implement-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!