<?php
final class Constants {
public function __get($constName)
{
// Null for non-defined "constants"
$val = null;
switch($constName){
case 'FOO':
$val = 'MY CONSTANT UNCHANGEABLE VALUE';
break;
case 'BAR':
$val = 'MY OTHER CONSTANT VALUE';
break;
}
return $val;
}
public function __set($key, $value) {
throw new Exception('A runtime modification of constant values is not allowed.');
}
}
$c = new Constants;
echo $c->FOO;
$c->FOO = 'Hello World';
MY CONSTANT UNCHANGEABLE VALUE
Fatal error: Uncaught Exception: A runtime modification of constant values is not allowed. in /in/UgEEm:23
Stack trace:
#0 /in/UgEEm(29): Constants->__set('FOO', 'Hello World')
#1 {main}
thrown in /in/UgEEm on line 23
Process exited with code 255.
MY CONSTANT UNCHANGEABLE VALUE
Fatal error: Uncaught exception 'Exception' with message 'A runtime modification of constant values is not allowed.' in /in/UgEEm:23
Stack trace:
#0 /in/UgEEm(29): Constants->__set('FOO', 'Hello World')
#1 {main}
thrown in /in/UgEEm on line 23
Process exited with code 255.
Output for 5.1.0 - 5.1.2
MY CONSTANT UNCHANGEABLE VALUE
Fatal error: Uncaught exception 'Exception' with message 'A runtime modification of constant values is not allowed.' in /in/UgEEm:23
Stack trace:
#0 /in/UgEEm(29): Constants::__set('FOO', 'Hello World')
#1 {main}
thrown in /in/UgEEm on line 23
Process exited with code 255.
Output for 5.0.3 - 5.0.4
MY CONSTANT UNCHANGEABLE VALUE
Fatal error: Uncaught exception 'Exception' with message 'A runtime modification of constant values is not allowed.' in /in/UgEEm:23
Stack trace:
#0 /in/UgEEm(23): Constants::__set()
#1 /in/UgEEm(29): Constants->__set()
#2 /in/UgEEm(29): unknown()
#3 {main}
thrown in /in/UgEEm on line 23
Process exited with code 255.
Output for 5.0.0 - 5.0.2
MY CONSTANT UNCHANGEABLE VALUE
Fatal error: Uncaught exception 'Exception' with message 'A runtime modification of constant values is not allowed.' in /in/UgEEm:23
Stack trace:
#0 /in/UgEEm(29): Constants->__set()
#1 /in/UgEEm(29): unknown()
#2 {main}
thrown in /in/UgEEm on line 23
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CLASS in /in/UgEEm on line 3
Process exited with code 255.