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.

6
  • You haven't declared $errorMsgs as a class variable. Commented Apr 11, 2020 at 0:48
  • It should also be $this->displayErrors instead of just $displayErrors Commented Apr 11, 2020 at 0: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. Commented Apr 11, 2020 at 0:56
  • 1
    That's because getAge() doesn't return an object. It either returns an int ($this->age) or an array ($this->errorMsgs). Commented Apr 11, 2020 at 1:01
  • 1
    Also, the code you're modeling yours from is 8 years old and isn't valid anymore. Commented Apr 11, 2020 at 1:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.