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;
  3.  
  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') );
  8.  
  9.                 // or use self
  10.                 var_dump( defined('self::CONST_THAT_DOES_EXIST') );
  11.             }
  12.  
  13.         }

webdevetc profile pic
webdevetc

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



More...


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 output (echo/print) content in PHP

What is the difference between reference and primitive data types types in Javascript?

What is the loop variable in a @foreach loop in Blade?

How to hide certain file types from directory listings

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?

How to have a whitelist of IP addresses that can access files in .htaccess

What is the difference between integration and unit tests?

How to install and use jpegoptim on Linux CentOS

How to find out what version of Laravel you are using?

How to find the intersection (same values) from two arrays?