2007/02 6
在各种语言中,define是不能把一个变量值define给一个常量的,如以下语法:

$url = 'www.rollenc.com'
define('URL', $url);
 

这样会出现错误Parse error错误。
但是你可以使用类的方法来赋值一个变量给常量,如以下代码:

class A
{
        var $v = 'hello';
        function setV($str)
        {
                $this->v = $str;
        }
}
$o = new A();
$o->setV('hello2');
define('A', $o->v);
echo A;
 

输出结果为hello2。
注意,这个hello2是一个可变量。

关于常量的一些知识:
  • 常量前面没有美元符号($);

  • 常量只能用 define() 函数定义,而不能通过赋值语句;

  • 常量可以不用理会变量范围的规则而在任何地方定义和访问;

  • 常量一旦定义就不能被重新定义或者取消定义;

  • 常量的值只能是标量(boolean,integer,float 和 string)。



知识来源:
Joomla源代码
php Manual
Defined tags for this entry:

Posted by rollenc

0 Trackbacks

  1. No Trackbacks

0 Comments

Display comments as(Linear | Threaded)
  1. No comments

Add Comment


You can use [geshi lang=lang_name [,ln={y|n}]][/lang] tags to embed source code snippets
E-Mail addresses will not be displayed and will only be used for E-Mail notifications