无法在类中将PDO对象初始化为属性 [英] Can't initialize a PDO object in a class as a property
本文介绍了无法在类中将PDO对象初始化为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
将属性声明为对象?
class core
{
public $dbh = new PDO("mysql:dbname=newdbnaem;host=1.1.1.1:1111", "owner", "passwordlulz");
function superman() {}
}
函数的结束标记。
推荐答案
只有常量值可以用作类属性的初始值。在构造函数中执行:
Only constant values can be used as initializers for class properties. Do it in the constructor:
class core {
public $dbh = null;
public function __construct() {
$this->dbh = new PDO("mysql:dbname=newdbnaem;host=1.1.1.1:1111", "owner", "passwordlulz");
}
}
这个声明可以包括一个初始化,但是这个初始化必须是一个常量值 - 也就是说,它必须能够在编译时被评估,并且不能依赖运行时信息来进行评估。
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
http://www.php.net/manual/en/language.oop5.properties.php
这篇关于无法在类中将PDO对象初始化为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文