- 类常量
<?php
class myuser
{
const age=34;//定义的常量不带$符号,常量之前没有访问修饰符,常量之前没有修饰符号
public function monolog()
{
echo "今年".self::age,"岁了,想做程序员,还有戏么"."
";
echo "什么?你已经".$this::age."了,都?"."
";
}
}
$dy = new myuser();
$dy->monolog();
echo "今年我".$dy::age."岁了"."
";
echo "今年我".myuser::age."岁了"."
";
?>
输出结果:
今年34岁了,想做程序员,还有戏么
什么?你已经34了,都?
今年我34岁了
今年我34岁了
- 静态变量---静态变量和方法属于类,于是不能出现$this->静态方法名,然而却是可以通过对象obj::静态方法名的方式访问。
<?php
class wenwa
{
public static $rules = "不准用手机"."
";
static public $name = "my name is taliban"."
";
public static function self_introduce()
{
echo "there is a game of love"."
";
}
}
$duwa = 'wenwa';
$saiwa = new $duwa;
$saiwa::self_introduce();
echo wenwa::$name;
?>
输出结果:
there is a game of love
my name is taliban