How to check if a object has a static variable defined?

PHP: How to check if a object has a static variable defined?

If you have an object and you need to check if it has a const SOME_CONSTANT = 'foo' set, then you can use defined in the following way:

  1.         class ExampleClass {
  2.             const CONST_THAT_DOES_EXIST = 9;
  4.             function does_it_have_const() {
  5.             // late static binding:
  6.                 var_dump( defined('static::CONST_THAT_DOES_EXIST') );
  7.                 var_dump( defined('static::CONST_THAT_DOES_NOT_EXIST') );
  9.                 // or use self
  10.                 var_dump( defined('self::CONST_THAT_DOES_EXIST') );
  11.             }
  13.         }

webdevetc profile pic

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This ( is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.


Comments and discussion about How to check if a object has a static variable defined?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to hide certain file types from directory listings

What is the max size, by default, of GET variables?

How to install and use jpegoptim on Linux CentOS

How to hide the server signature (hide Apache info) from HTTP headers with .htaccess

How does PHP compare objects?

How to enable the rewrite engine in .htaccess?

How to reverse htmlentities() / What is the opposite of htmlentities()?

How to set up custom error pages in .htaccess for your Apache server (ErrorDocument)

How to return JSON data to the user in Laravel?

How to pick what array_intersect function to use in PHP?