phpクラスでconstとdefineを使って定数を定義する方法

phpコード例1
<?php
define(“STUDYPHP","startnews24″);
class CftConstDemo
{
const constant = 'constant value’;

function showConstant() {
echo self::constant . “<br>";
}
}

echo CftConstDemo::constant . “<br>";

$classname = “CftConstDemo";
echo $classname::constant . “<br>";

$class = new CftConstDemo();
$class->showConstant();
echo $class::constant."<br>";
//print_r(get_defined_constants());
?>

phpコード例2
<?php
if(1){
const a = 'java’;
}
echo a; //NG
?>

phpコード例3
<?php
const FOO = 'STUDYPHP’;

for ($i = 0; $i < 32; ++$i) {
define('STUDYPHP_’ . $i, 1 << $i);
}
?>

phpコード例4
<?php
const PHP = 2 << 6; // NG
define('PHP’, 2 << 6); // OK
?>

PHP

Posted by arkgame