5

How I can destroy an const? I tried unset( KEY ), but not work.

<?php
define('KEY', 'value');
echo KEY; //output value
unset( KEY ); //no work
?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mark Neto
  • 145
  • 4
  • 10
  • 4
    The whole point of a constant is that it is immutable after it has been defined. Since you cannot change its value, there is little point to being able to unset it. – Michael Berkowski Jun 23 '12 at 03:24

6 Answers6

8

Constants created with define() can't be undefined once created.

matt3141
  • 4,303
  • 1
  • 19
  • 24
4

The closest you can get to unsetting a constant is use of the RunKit extension. See http://php.net/manual/en/function.runkit-constant-remove.php

0

You can't destroy a constant or alter its value during run time.

Your best option is to use a variable.

$key = "value";

echo $key;

unset($key);

Not to mention; If you could, it would be extremely bad practice :P

Menztrual
  • 40,867
  • 12
  • 57
  • 70
0

This isn't possible. If you could unset a constant then it wouldn't be very constant!

If you really need to delete this constant you probably need to use a variable.

user1474090
  • 675
  • 6
  • 5
0

You can't destroy or unset a Constant in PHP and it's value can't change along de Script.

DrWaky
  • 1,165
  • 8
  • 7
0

Use

runkit_constant_remove ( 'KEY' );

http://php.net/manual/es/function.runkit-constant-remove.php

nacesprin
  • 392
  • 7
  • 16