Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 889
Florian Müller Avatar asked May 04 '26 15:05

Florian Müller


2 Answers

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.

like image 70
Alex Howansky Avatar answered May 06 '26 06:05

Alex Howansky


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
   }
like image 34
hohner Avatar answered May 06 '26 05:05

hohner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!