Redefining constants in PHP
If you have the runkit extension installed, you can use runkit_constant_redefine()
:
runkit_constant_redefine("name", "value");
In most circumstances, however, it would be a better idea to re-evaluate why you're using constants and not something more fitting.
Is it possible to redefine PHP constants?
No, of course not. Then they wouldn't be "constants."
I can redefine constant in PHP without getting an error
To report all php error
error_reporting(E_ALL);
To report all php errors except notices
error_reporting(E_ALL & ~E_NOTICE);
And in your case, you are redefining a constant that has been already defined show PHP throws a notice. By using
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
You are telling PHP not to report any kind of notices or warnings. You should remove them.
To get all warning and notices use
error_reporting(E_ALL);
Fore more information refer to the official doc, and have a look on examples.
Can you undefine or change a constant in PHP?
No. Constants are constant.
Reference: php.net/manual/language.constants.php
Change the value of a previously-defined constant
From http://php.net/manual/en/function.define.php (emphasis is mine):
define
— Defines a named constant
From http://www.php.net/manual/en/language.constants.php:
As the name suggests, that value cannot change during the execution of the script
Redefining Codeigniter slug-based constants in the model per user
It not so much "unset" as "not yet defined". Each browser request to a URL (controller) is a unique process in the server. So the constant is not redefined but is newly instantiated for each URL request.
If you try to run $this->main_model->set_slug_based_constants($slug);
a second time you will get a PHP error message stating that Constant CLOUDFRONT_DIRECTORY already defined.
Try this version of the controller to see the error.
class Content extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('main_model');
$this->main_model->set_slug_based_constants("Dave");
}
function index()
{
echo "The constant's value is <strong>".CLOUDFRONT_DIRECTORY."</strong><br>";
$this->main_model->set_slug_based_constants("jruser");
echo "The constant's value is <strong>".CLOUDFRONT_DIRECTORY."</strong><br>";;
}
}
It will produce output something like this.
The constant's value is Dave_cloudfront_directory
A PHP Error was encountered
Severity: Notice
Message: Constant CLOUDFRONT_DIRECTORY already defined
Filename: models/Main_model.php
Line Number: 13
The constant's value is Dave_cloudfront_directory
How to override a class constant in PHP?
If your users are extending your class, they can redefine the constant just fine:
class Foo extends Bar {
const BAZ = 42;
}
It doesn't matter whether Bar
already defines a BAZ
constant or not. All you have to do in your code is make sure you're referring to the "current classes" constant using late static binding:
public function useConst() {
echo static::BAZ;
}
Otherwise, you have the option of doing this:
if (!defined('BAZ')) {
define('BAZ', 42);
}
class Bar {
const BAZ = BAZ;
}
That's not really very elegant though. You should rather be using getters and setters on your class to allow configuration, rather than using constants for this purpose:
Bar::setBaz(42);
unset or change a defined constant PHP
When you use PHP's define() function, you're not defining a variable, you're defining a constant. Constants can't have their value modified once the script starts executing.
Related Topics
Using Ajax to Pass Variable to PHP and Retrieve Those Using Ajax Again
Wordpress: Loading Multiple Scripts with Enqueue
How to Compare Two Time in PHP
Zf2 - Get Controller Name into Layout/Views
Run a Ffmpeg Process in the Background
Send Data from Android to Server via JSON
Remove Trailing Slash from String PHP
Strict Standards: Only Variables Should Be Assigned by Reference PHP 5.4
Does the Order of Class Definition Matter in PHP
How to Call a PHP Function from Ajax
Undefined Index While Uploading File
Pass Array Literal to Postgresql Function
How to Validate a Domain Name Using Regex & PHP
How to Pass an Array Using PHP & Ajax to JavaScript
PHP Files Are Downloaded by Browser Instead of Processed by Local Dev Server (Mamp)