Heim  >  Artikel  >  Backend-Entwicklung  >  php 面向对象的问题

php 面向对象的问题

WBOY
WBOYOriginal
2016-09-27 14:18:081112Durchsuche

<code>class testClass{

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

//.....
}

}
</code>

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

回复内容:

<code>class testClass{

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

//.....
}

}
</code>

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

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

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

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

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

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

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

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn