PHP 变量和函数的使用
定义常量的两种方法
| const $a = b; // 新版
define('a', b); // 旧版
|
引用
| require $a;
// 还有
include $a;
|
每次被引用时都会被执行,可能会有函数重复引用的风险。使用下面的语句,可以避免这样的情况。
| require_once $a;
// 还有
include_once $a;
|
变量的值和函数名
| <?php
function hello(){
return 'world';
}
$a = 'hello';
echo $a . '<br>';
echo $a();
?>
|
以下为浏览器中输出:
命名空间
为了防止引入文件时类名重复,在 php 文件的 <?php 之后加入 namespace a; 即可设置命名空间为 a。
引用后可以在类前加上 \a\ 来表明属于哪个命名空间。
| <?php namespace a;
$d = new \a\b();
|