PHP Extensions: Creating Constants

by BlakeS on December 18, 2008

When creating extensions, it is possible to provide the end-programmer with new constants. Such constants may be then used as parameters to extension functions or to compare against return values. The following table shows the macros that are available:

Macro Description
REGISTER_LONG_CONSTANT(name, value, flags)
REGISTER_MAIN_LONG_CONSTANT(name, value, flags)
Registers a new constant of type long.
REGISTER_DOUBLE_CONSTANT(name, value, flags)
REGISTER_MAIN_DOUBLE_CONSTANT(name, value, flags)
Registers a new constant of type double.
REGISTER_STRING_CONSTANT(name, value, flags)
REGISTER_MAIN_STRING_CONSTANT(name, value, flags)
Registers a new constant of type string. The specified string must reside in Zend’s internal memory.
REGISTER_STRINGL_CONSTANT(name, value, length, flags)
REGISTER_MAIN_STRINGL_CONSTANT(name, value, length, flags)
Registers a new constant of type string. The string length is explicitly set to length. The specified string must reside in Zend’s internal memory.

Table 9: Macros for Creating Constants

There are two types of macros – REGISTER_*_CONSTANT and REGISTER_MAIN_*_CONSTANT. The former type creates constants bound to the extension module. These constants are dumped from the symbol table as soon as the module that registered the constant is unloaded from memory. The latter type creates constants that persist in the symbol table independently of the module.

Each of the macros above accepts a name and value (and a length for the STRINGL versions) and a set of flags. The flags available for use are:

  • CONST_CS – This constant’s name is to be treated as case sensitive
  • CONST_PERSISTENT – This constant is persistent and won’t be forgotten when the current process carrying this constant is shut down

A simple usage example is shown below:

1
2
// register a new constant of type "long"
REGISTER_LONG_CONSTANT("MY_CONSTANT", 1234, CONST_CS | CONST_PERSISTENT);

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>