Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP appears to define null as itself. How does this work?

Tags:

php

null

A colleague of mine discovered, by ctrl-clicking on null in PHPStorm, that PHP defines null as

define ('null', null, true);

in core_d.php.

  1. Why does PHP need to define null at all?
  2. How does this even work? How can it define null as itself?

My best theory so far is that perhaps null is defined already, but using define allows adding a case-insensitive alias, so to speak. I don't know enough about PHP to judge the likelihood of this.

like image 804
Clonkex Avatar asked Jan 01 '23 03:01

Clonkex


1 Answers

core_d.php belongs to PHPStorm not PHP itself. See the file on github. PHP is written almost entirely in C. The file probably exists to help with code insights and static analysis. From the README file of the stubs repo:

STUBS are normal, syntactically correct PHP files that contain function & class signatures, constant definitions, etc. for all built-in PHP stuff and most standard extensions. Stubs need to include complete PHPDOC, especially proper @return annotations.

An IDE needs them for completion, code inspection, type inference, doc popups, etc. Quality of most of these services depend on the quality of the stubs (basically their PHPDOC @annotations).

However, as pointed out by Bartosz Zasada php does define a constant called null. It is currently defined in line 137 of ext/zend/zend_constants.c in the php source code at the end of zend_register_standard_constants().

like image 80
kaan_a Avatar answered Jan 08 '23 01:01

kaan_a