Add to Favorites

PHP testing if a function exists

In php you may need to check certain things are available before you call them to prevent errors or to change programming behavior depending on what functionality an object has that is passed.

There are a couple functions for testing the existence of functions or variables:

  • function_exists('func_name')
  • isset($var)

There are a couple other ones used for testing that an object/class has a method or member:

  • method_exists($obj,'func_name')
  • property_exists($obj,'var_name')

You can also use the object functions with the class name simply providing two strings to the method. The first string being the class name.

There are other useful functions for php objects that you can use to change programming behavior depending on the type of object passed. You could use the is_a($obj,'class_name') function to determine if an object passed is a specific class, or has a specific class as a parent.

Comments

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up