Constants

This chapter covers how to define a global constant.

Code sample for this chapter, with additional examples, is available here.

Userland PHP Code Snippet

The code below shows how a constant would be declared in userland PHP code.

const HELLO = 'World!';

Internal PHP Code

The following sections show all required code to declare and implement a constant internally in PHP.

PHP Stub (hdi.stub.php)

The stub file should not declare global constants as the stub generator script (gen_stub.php) cannot handle it at this moment.

Argument Information (hdi_arginfo.h)

Constants are not referred to nor registered in the argument information file, so it can be skipped.

Implementation (hdi.c)

The implementation below is very simple and straight forward:

  • declaration (REGISTER_MAIN_STRING_CONSTANT)
    • name string ("HELLO")
    • value string ("World!")
    • flags (CONST_CS and CONST_PERSISTENT)
REGISTER_MAIN_STRING_CONSTANT("HELLO", "World!", CONST_CS | CONST_PERSISTENT);

Macros and Functions:

References:

Note that constants must be registered during the MINIT() stage.


Copyright (c) 2022 - Flavio Heleno