码迷,mamicode.com
首页 > 其他好文 > 详细

静态变量

时间:2014-05-26 14:13:28      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:c   class   a   get   cti      

 

例子
<?php
class test
{
  const constvar=‘hello world‘;
  static $staticvar=‘hello world‘;
  function getStaticvar(){
     return self::$staticvar;
  }
}

$obj=new test();
echo test::constvar //输出‘hello world‘
echo test::staticvar //出错,staticvar 前必须加$才能访问,这是容易和类常量(per-class常量)容易混淆的地方之一
echo test::$staticvar //输出‘hello world‘
$str=‘test‘;
echo $str::$staticvar //出错,类名在这不能用变量动态化
echo $str::constvar //出错原因同上

//在类名称存在一个变量中处于不确定(动态)状态时,只能以以下方式访问类变量
$obj2=new $str();
echo $obj2->getStaticvar();
?>

静态变量,布布扣,bubuko.com

静态变量

标签:c   class   a   get   cti      

原文地址:http://www.cnblogs.com/fg19/p/3746487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!