0

I'm trying to validate user data input using PHP OOP and display errors if they exist. However, this doesn't seem to be working.

This is my class:

class Forms {

    private int $age;
    private int $year;
    private $output = array();
    public $displayErrors = array();

    function getAge() {
        if (is_int($this->age)) {
            return $this->age;
        } else {
            //$this->age = 'null';
            $this->errorMsgs = array('error' => 'The output is not a number',);
            return $this->errorMsgs;
        }
    }

    function setAge($age) {
        $this->age = $age;
    }


    public function displayErrors() {
        $displayErrors[] = Forms::getAge()->$errorMsgs;
        return $displayErrors;
    }
}

If $age is not a number I want it to display the error The output is not a number through an array. and return the array when displayErrors is called.

However I'm getting this error: Notice: Undefined variable: errorMsgs

I tried modelling my code on this reply.

ACDC
  • 119
  • 1
  • 7
  • You haven't declared `$errorMsgs` as a class variable. – Nick Apr 11 '20 at 00:48
  • It should also be `$this->displayErrors` instead of just `$displayErrors` – M. Eriksson Apr 11 '20 at 00:55
  • `Forms::getAge()`, the method `getAge()` isn't static, but you call it statically. It also returns either an int or an array, but you use it as an object `->$errorMsgs`. There's simply too many basic things wrong with the posted code. I would recommend reading up a bit more of the basics for PHP and OOP. – M. Eriksson Apr 11 '20 at 00:56
  • Hi, thanks for your replies. However I'm getting `Trying to get property 'errorMsgs' of non-object in $this->displayErrors = $this->getAge()->errorMsgs;` – ACDC Apr 11 '20 at 01:00
  • 1
    That's because `getAge()` doesn't return an object. It either returns an int (`$this->age`) or an array (`$this->errorMsgs`). – M. Eriksson Apr 11 '20 at 01:01
  • 1
    Also, the code you're modeling yours from is 8 years old and isn't valid anymore. – M. Eriksson Apr 11 '20 at 01:11

1 Answers1

0

Let's break this down:

  • $displayErrors = array() doesn't need to be public in its definition because you return it from a public function anyway
  • Are your getAge() and setAge() functions private? public? ... define them as such.
  • $this->errorMsgs = array('error' => 'The output is not a number',); is not valid because you're trying to assign to a class variable that doesn't exist. Either:
    1. Define the class variable with your other definitions so you can assign to it (private $errorMsgs;)
    2. Return the array on calling the function (else { return array('error' => 'The output is not a number'); })
  • $displayErrors[] = Forms::getAge()->$errorMsgs;
    1. getAge() is not static so cannot be called with Forms::getAge(), instead it should be called with self::getAge();
    2. getAge() returns different things based on whether $this->age == null or not. However it doesn't return an object so getAge()->$errorMsgs; doesn't work.

Here's a much simplified class that overcomes these issues:

class Forms {
  private $_age, $_year;

  public function setAge($age) {
    $this->_age = $age;
  }

  public function getAge() {
    // If age is null or not an int return null, otherwise return the value
    return (is_null($this->_age) || !is_int($this->_age)) ? null : $this->_age;
  }
}

Then you can see how this behaves when you set different values in setAge(), and handle cases where age is not valid using a simple if statement:

test(21);
test('test');
test(null);

function test($set_to) {
  // Instantiate the Forms object and set the age
  $form = new Forms();
  $form->setAge($set_to);

  // will be false if age is null
  if($age = $form->getAge()) {
    echo 'Age is '.$age.'</br>';
  } else {
    echo 'Age is not valid (null or non-int)</br>';
  }
}

You can find a live example of the code here. Be sure to click the little eye symbol in the output box to render the line breaks properly.

compuphys
  • 1,289
  • 12
  • 28