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

What is the difference between var, let and const in Javascript?

What does 'final class' and 'final function' (final method) mean in PHP?

How to check if an object or variable is an array, in JS?

What are Apache MPMs?

An overview of every main PHP array function

What is Bubble sort, and how to write an implementation of it in PHP

How to make Apache force download of files (in .htaccess)

How to force SSL in Apache's .htaccess?

How do Invokable Controllers work in Laravel?

How to Enable Directory Listings in Apache with .htaccess