TheCrownedPixel
TheCrownedPixel

Reputation: 369

Call to a member function count() on a non-object, Line 23

Having trouble understanding why I can't count() the $check variable in my code. The code is suppose to be checking to see if the sql has returned any values.

Please let me know if I need to submit anymore information.

<?php
class Validate {
    private $_passed = false,
            $_errors = array(),
            $_db = null;


    public function __construct() {
        $this->_db = DB::getInstance();
    }

    public function check($source, $items = array()) {
        foreach($items as $item => $rules) {
            foreach($rules as $rule => $rule_value) {
                $value = $source[$item];

                if($rule === 'required' && empty($value)) {
                    $this->addError("{$item} is required");
                } else if(!empty($value)){
                    switch($rule) {
                        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;
                    }
                }
            }
        }
        if(empty($this->_errors)) {
            $this->_passed = true;
        }
        return $this;
    }

    private function addError($error) {
        $this->_errors[] = $error;
    }

    public function errors() {
        return $this->_errors;
    }

    public function passed() {
        return $this->_passed;
    }
}
?>

Upvotes: 1

Views: 533

Answers (2)

Larry Borsato
Larry Borsato

Reputation: 394

You want to do count($check). $check is not a class.

Upvotes: 1

Pupil
Pupil

Reputation: 23958

As per your question, $count is a variable (array/variable) and you are calling a member function on it.

But, its not an object.

count() is in built PHP function.

You need to change:

if ($check->count()) {

to

if (count($check)) {

count()

Upvotes: 2

Related Questions