I use OOP and i wanted to ask you guys how this would be done! I keep trying but its still not working ;(
Here is my class file:
class Signup { // Error public $error = array(); public function validate($username, $email_mobile, $password) { if(!empty($username) || !empty($email_mobile) || !empty($password)){ if(strlen($username) < 3 || strlen($username) > 50){ $this->error = "Username is too short or too long!"; return $this->error; }elseif(strlen($email_mobile) < 3 || strlen($email_mobile) > 50) { $this->error = "Email is too short or too long!"; return $this->error; }elseif(strlen($password) < 3 || strlen($password) > 50){ $this->error = "Password is too short or too long!"; return $this->error; } } else { $this->error = "Please fill are required feilds"; return $this->error; } }
Here is my signup file
$error[] = $signup->validate($username, $email_mobile, $password); <?php // require('lib/function/signup.php'); if(isset($error)){ var_dump($error); foreach ($error as $value) { echo $value . "<br>"; } } ?>
I know That im calling the $error in the same file and not the property error. But i dont know how to send this array to the other file! Please help me! Also i have Called everything and the problem is just with my code(i think), i only included my file and made a var to call my signup class
Advertisement
Answer
It is never too early in your development career to study coding standards. Jump straight to PSR-12, and adopt all of these guidelines to write beautiful, professional code.
Use data type declarations in your classes where possible, it will improve the data integrity throughout your project(s).
You appear to prefer returning an array of errors. For this reason, I see no benefit in caching the errors long-term in a class property. This coding style is fine to do, but you could choose to return nothing (
void
) and instead populate a class property$errors
, then access it directly after the$signup->validate()
call via$signup->errors
or use a getter method.The
empty()
checks are too late in the flow. Once the values have been passed to the class method, these values must already be declared. For this reasonempty()
is needless overhead to check for mere “falsiness”. Just check the values’ string length.Your data quality checks seem a little immature (email and password checks should be much more complex), but I won’t confuse you with any new complexity, but I do expect that your validation rules will increase as you realize that users cannot be trusted to put good values in forms without be forced to do so. For this reason, it is probably unwise to use a loop to check the value lengths because you will eventually need to write individual rules for certain values.
A possible write up:
class Signup { public function validate( string $username, string $email, string $password ): array { $errors = []; $usernameLength = strlen($username); if ($usernameLength < 3 || $usernameLength > 50) { $errors[] = "Username must be between 3 and 50 characters"; } $emailLength = strlen($email); if ($emailLength < 3 || $emailLength > 50) { $errors[] = "Email must be between 3 and 50 characters"; } $passwordLength = strlen($password); if ($passwordLength < 3 || $passwordLength > 50) { $errors[] = "Password must be between 3 and 50 characters"; } return $errors; } }
When calling this method…
$signup = new Signup(); $errors = $signup->validate( $_POST['username'] ?? '', $_POST['email'] ?? '', $_POST['password'] ?? '' ); if ($errors) { echo '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>'; } else { echo 'No errors'; }