Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a SAPI function to define PHP constants #750

Open
adamziel opened this issue Nov 17, 2023 · 1 comment
Open

Add a SAPI function to define PHP constants #750

adamziel opened this issue Nov 17, 2023 · 1 comment
Labels

Comments

@adamziel
Copy link
Collaborator

Let's provide a method such as define to enable defining PHP constants for every subsequent request:

php.define('DB_NAME', 'dev');

This would enable setting up wp-config.php constants without patching WordPress or creating preloaded files like in the DefineWpConfigConstsStep.

adamziel added a commit that referenced this issue Nov 17, 2023
#738 introduced a
change that made all WordPress constants be defined by including a
"virtualized" wp-consts.php file. However, the required
`setPhpIniEntry()` call was sometimes happening after the initial
`php.run()` which made it noop. This broke the networking support merged
in #724.

This commit solves that by exposing a new `php.defineConstant()` API
that handles the file virtualization. I'm not super happy about
virtualizing files so let's revisit that in the future, see #750.
@adamziel adamziel mentioned this issue Nov 22, 2023
26 tasks
@adamziel
Copy link
Collaborator Author

adamziel commented Dec 6, 2023

Relevant note from http://php.adamharvey.name/manual/en/internals2.ze1.zendapi.php:

Creating Constants

Zend supports the creation of true constants (as opposed to regular variables). Constants are accessed without the typical dollar sign ($) prefix and are available in all scopes. Examples include TRUE and FALSE, to name just two.

To create your own constants, you can use the macros in Macros for Creating Constants. All the macros create a constant with the specified name and value.

You can also specify flags for each constant:

  • 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 shuts down.

To use the flags, combine them using a inary OR:

     // register a new constant of type "long"
     REGISTER_LONG_CONSTANT("NEW_MEANINGFUL_CONSTANT", 324, CONST_CS |
     CONST_PERSISTENT); 

There are two types of macros - REGISTER_CONSTANT and REGISTER_MAIN_CONSTANT. The first type creates constants bound to the current module. These constants are dumped from the symbol table as soon as the module that registered the constant is unloaded from memory. The second type creates constants that remain in the symbol table independently of the module.

Macros for Creating Constants

REGISTER_LONG_CONSTANT(name, value, flags) 
REGISTER_DOUBLE_CONSTANT(name, value, flags)
REGISTER_STRING_CONSTANT(name, value, flags) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant