日期:2014-05-17  浏览次数:20489 次

子类使用父类变量
class A {
var $authKey='1111';

}

class B extends A{
 __construct(){
  echo parent::authKey;
}
}
报错Undefined class constant 'authKey'

------解决方案--------------------
错误信息的意思是未定义的常量。你少了个$
但$authKey不是静态变量,所以你不能这么静态调用
正确的做法
PHP code
class A {
   // 不建议类中用var来声明变量
   public $authKey='1111';
}

class B extends A{
   // B将拥有A的所有非private成员
   public function __construct(){
      echo $this->authKey;
   }
}

------解决方案--------------------
var 是在php4以前的版本,后来就省略了。
------解决方案--------------------
var最好还是视情况带上!有些低版本支持
------解决方案--------------------
var 是php 4.X中的,,,5+里使用是为了向下兼容,,

新写的程序,基本可以放弃这种写法了

你的代码也可以这样用
PHP code

class A {
const authKey='1111';

}

class B extends A{
 public function __construct(){
  echo parent::authKey;
}
}

new B;