登录

php 面向对象的问题

class testClass{

public $str_md5 = md5('fefsffa');
public static function testFunction(){

//.....
}

}

我的问题是: md5()函数 如上所示使用 为什么会报错?? php 面向对象 中 属性不能用 php自带的方法吗???

# PHP
阿神阿神2153 天前500 次浏览

全部回复(4) 我要回复

  • 天蓬老师

    天蓬老师2017-04-11 10:33:36

    因为文档就明确规定不能这样声明属性。

    http://php.net/manual/en/lang...

    回复
    1
  • PHPzhong

    PHPzhong2017-04-11 10:33:36

    初始化不可以用函数的(只能用常量),你可以在__constrct里面对$this->str_md5初始化

    回复
    0
  • 巴扎黑

    巴扎黑2017-04-11 10:33:36

    类的属性不可以直接使用函数赋值吧。
    例如在类中,
    class One{

    public $str = 'abc'; //这样没问题
    public $str = md5('abc'); //使用函数赋值则会报错

    }
    需要定义属性后,再进行赋值。

    回复
    0
  • 天蓬老师

    天蓬老师2017-04-11 10:33:36

    静态方法不能调用非静态属性

    回复
    0
  • 取消回复发送