致命错误:在非对象中调用成员函数get()

时间:2014-01-24 00:40:57

标签: php

我收到以下错误:致命错误:

中的非对象上调用成员函数get()

我创建了一个公共函数get(),以检索已查询的sql数据。

我目前正在进行验证阶段,并且我使用get函数检查数据库以查看帐户电子邮件是否已注册。

我通过数组创建了以下规则:

$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;


                }
            }
        }
    }

所有案例都有效,直到我进入get()函数。我不确定是什么问题。有什么建议吗?

Alibi,我的抱歉,

这是我在课程开始时所做的:

class Validation{
private $_passed = false,
        $_errors= array(),
        $_db = null;

public function _construct(){
    $this->_db = DB::getInstance();
}/*end _construct*/

通过添加私有变量_db,我希望从单例模式中存储DB :: getInstance()函数。

感谢百万

1 个答案:

答案 0 :(得分:1)

这意味着$this->_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() {

  }
}