0

I dont understand why this is not working and any advice for oop im beginner on oop paradigm

My class

class formvalidation  {

     public function check_empty_input($inputs=[]){
         $checked = false;

         foreach($inputs as $valor){
             if(empty($valor)){
                 $checked = false;
                 break;  
             } else {
                 $checked = true;
             }
         }

         if($checked = true){return true;}
         else {return false;}
     }

}

Check if posts are empty

  $formvalidation= new formvalidation();

  $inputs = array($_POST['name'],$_POST['email'],$_POST['pass'],$_POST['confirmpass']);
  if($formvalidation->check_empty_input($inputs)) 
ryantxr
  • 4,119
  • 1
  • 11
  • 25
AlmostDone
  • 21
  • 1
  • 7

1 Answers1

0

Well, the problem is in the return if, you are using = as comparison operator, where instead you should have used ==, and so the function will return always true... also you should use a static function from the moment that you don't need a object to invoke this function, try with this one:

<?php

class formvalidation {
    public static function check_empty_input($inputs = []) : bool {
        $everything_filled = true; //start with this supposition
        foreach ($inputs as $valor) {
            if (empty($valor)) {
                $everything_filled = false; // is something is empty, than the supposition is false
                break;
            }
        }
        return $everything_filled; // return the supposition result value
    }
}

$is_my_inputs_filled = formvalidation::check_empty_inputs([
    $_POST['name'],
    $_POST['email'],
    $_POST['pass'],
    $_POST['confirmpass'],
]);

If it doesn't work, please explain better what you mean with "doesn't work"

Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • Thank you... I did not see that was just only one '=' thank you for wake me up i was thinking my logic was bad or something didnt even check that part!! – AlmostDone Dec 14 '19 at 02:47
  • no the logic wasn't wrong, the else statement was useless, and the return statement can be replaced as shown in the answer, if this solves you problem, please set as answer to close the question – Alberto Sinigaglia Dec 14 '19 at 02:48