How to change PHP constants?

I am working on creating my own custom CMS in PHP manually, and I have a few constants that I defined. Is there an easy way to change constants? I thought about using something like fopen(), and then changed it, but I never used the file system functions.

+5
source share
8 answers

A constant is an identifier (name) for a simple value. As the name suggests, the value cannot change at run time (except for magic constants, which are not actually constants). By default, a constant is case sensitive. By convention, constant identifiers are always capitalized.

, PHP. , , . , : [A-Za-Z_\x7f-\XFF] [A-Za-z0-9_\x7f-\XFF] *

from: http://php.net/manual/en/language.constants.php

+26

. , allow_url_fopen, , . PHP,

UPDATE

, , , : 0. sg. %%constant1. 1. . 2. . 3. str_replace , str_replace ( "%% constant1", $_ POST [ "value1" ], $configfile). 4. $configfile .

2

CMS : , . , , , IDK, .

+15

. , " constant - , " (). , . The Fine Manual, :

[] script.

+14

.

+6
in PHP, but only if you have the runkit extension installed.

However, I must question why you're even defining a constant if you need to change it.  It would no longer be a constant once changed and it appropriate to use constants in your context if it needs to be changed.  You may want to consider making it a global variable if it needs to be accessible in other functions or parts of your code, or write proper code by:
Passing the data as parameters to a function, then returning the manipulated values (if coding in a procedural fashion).
Set the value in a class' property, then access the value using $this within your class (if coding in an OO fashion).
+1

, .

0

, , . , :

define('ROOT', '/some/path');
echo ROOT; // echo /some/path
define('ROOT', '/some/other/path'); // gives an error
echo ROOT; // gives /some/path (if there were no error

$root = '/some/path';
echo $root; // echo /some/path
$root = '/some/other/path';
echo $root; // echo /some/other/path
0

, script, , CMS, . hes .

, :

define(DB, '');
define(USER, '');
define(PASS, '');
define(HOST, '');

And he wants to create scripts that populate these constants with data so that they can be used in the CMS.

0
source

All Articles