CroStorm99
CroStorm99

Reputation: 154

OOP PHP call to a member function on null

I am getting this error

Fatal error: Call to a member function passed() on null

but can't find mistake, I already looked at other posts and didn't find useful.

So I have 2 files, first register.php

<?php

require_once 'core/init.php';

if(Input::exists()){
    $validate = new Validate();
    $validation = $validate->check($_POST, array(
        'username' => array(
            'required' => true,
            'min' => 2,
            'max' => 20,
            'unique' => 'users'
        ),
        'password' => array(
            'required' => true,
            'min' => 6
        ),
        'password_again' => array(
            'required' => true,
            'matches' => 'password'
        ),
        'name' => array(
            'required' => true,
            'min' => 3,
            'max' => 50
        )
    ));

    if($validation->passed()){
        echo 'Passed';
    }else{
        print_r($this->errors());
    }
}

?>

and second Validate.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($this->_errors)){
            $this->_passed = true;
        }
    }

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

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

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

}

It shows me error in first file in if($validation->passed()){

thanks for help

Upvotes: 1

Views: 5339

Answers (3)

Fullmetal
Fullmetal

Reputation: 1

Your check() function should return something since it is not returning anything hence the NULL error.

To solve this add return $this; at the end of your check() function, so it would looks something like this.

    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($this->_errors)){
        $this->_passed = true;
    }
  return $this;    //add this statement
}

Upvotes: 0

Manfred Radlwimmer
Manfred Radlwimmer

Reputation: 13394

You function $validate->check doesn't return anything, so your $validation stays null. When you then call $validation->passed(), $validation is still null. Thats why the error says

Fatal error: Call to a member function passed() on null

because you are trying to call function passed on null. What you probably intended to do is:

if(Input::exists()){
    $validate = new Validate();
    $validate->check($_POST, array(
        'username' => array(
            'required' => true,
            'min' => 2,
            'max' => 20,
            'unique' => 'users'
        ),
        'password' => array(
            'required' => true,
            'min' => 6
        ),
        'password_again' => array(
            'required' => true,
            'matches' => 'password'
        ),
        'name' => array(
            'required' => true,
            'min' => 3,
            'max' => 50
        )
    ));

    if($validate->passed()){
        echo 'Passed';
    }else{
        print_r($validate->errors());
    }
}

Upvotes: 1

u_mulder
u_mulder

Reputation: 54841

Replace

if($validation->passed()){

with

if($validate->passed()){

And also replace

$this->errors()

with

$validate->errors()

As object of Validate class is $validate and not $validation.

Upvotes: 2

Related Questions