Php 致命错误:对中的非对象调用成员函数get()
我收到以下错误:致命错误: 对中的非对象调用成员函数get() 我创建了一个公共函数get(),用于检索已查询的sql数据 我目前正在进行验证阶段,我加入了get函数来检查数据库,查看帐户电子邮件是否已经注册 我通过数组创建了以下规则:Php 致命错误:对中的非对象调用成员函数get(),php,Php,我收到以下错误:致命错误: 对中的非对象调用成员函数get() 我创建了一个公共函数get(),用于检索已查询的sql数据 我目前正在进行验证阶段,我加入了get函数来检查数据库,查看帐户电子邮件是否已经注册 我通过数组创建了以下规则: $validation = $validate->check($_POST, array( 'fName' => array(
$validation = $validate->check($_POST, array(
'fName' => array(
'required' => true,
'min' => 2,
'max' => 20),
'lName' => array(
'required' => true,
'min' => 2,
'max' => 20),
'regEmail' => array(
'required' => true,
'min' => 2,
'max' => 50,
'unique' => 'customers'),
'regEmailCon' => array(
'required' => true,
'min' => 2,
'max' => 50,
'matches' => 'regEmail'),
'regPword' => array(
'required' => true,
'min' => 6,
'max' => 12),
'regPwordCon' => array(
'required' => true,
'matches' => 'regPword'),
));
以下是用于执行验证的代码:
public function check($source, $items = array()){
foreach($items as $item => $rules ){
foreach ($rules as $rule => $rule_value){
$value = trim($source[$item]);
$item = escape($item);
if($rule === 'required' && empty($value)){
$this->addError("{$item} is required");
}elseif (!empty($value)){
switch ($rule){
case 'min':
if(strlen($value) < $rule_value){
$this->addError("{$item} must be a minimum of {$rule_value} characters.");
}
break;
case'max':
if(strlen($value) > $rule_value){
$this->addError("{$item} must be a maximum of {$rule_value} characters.");
}
break;
case 'matches':
if ($value != $source[$rule_value]){
$this->addError("{$rule_value} must match {$item}");
}
break;
case'unique':
$check = $this->_db->get($rule_value, array("$item", '=', "$value"));
if ($check->count()){
$this->addError("{$item} already exists.");
}
break;
}
}
}
}
通过添加私有变量_db,我希望存储singleton模式中的db::getInstance()函数
感谢一百万这意味着
$this->\u db
不是一个具有名为get
的方法的对象。为了让代码正常工作,您需要这样的东西:
在具有函数check的类中:
public function __construct($_db) {
//this is how `check` will get access to a `db` object as a property ("this->$_db")
$this->$_db = $_db;
}
实例化该类时:
$_db = new db(); //this has a method "get"
//pass the $_db object to the class
$myClass = new MyClass($_db);
以及db
类:
class db {
//this is where the db object is given a method "get"
public function get() {
}
}
$this->\u db指的是什么?在这个函数的范围内?单例模式不是好的实践。相反,创建一个新的数据库对象并将其传递给构造函数。这种良好的实践称为依赖注入。
class db {
//this is where the db object is given a method "get"
public function get() {
}
}