Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4621900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:46:03+00:00 2026-05-22T02:46:03+00:00

i have a php class method which determines whether if a class property have

  • 0

i have a php class method which determines whether if a class property have got any value. if it holds any value then it will validate and iterate $this->error class property. here is the class method i am using.

public function validate() {
    if(!empty($this->name)) {
        if(!preg_match('/^[a-zA-z ]{3,50}$/',$this->name)) {
            $this->error['name'] = 'Name should be valid letters and should be between 3 and 25 characters';
        }
    }
    if(!empty($this->email)) {
        if(!filter_var($this->email,FILTER_VALIDATE_EMAIL)) {
            $this->error['invalidEmail'] = 'Invalid email address';
        }
        if(empty($this->userId) && $this->emailCount($this->email)) {
            $this->error['emailExist'] = 'Email already exist';
        }
    }
    if(empty($this->userId) && !empty($this->password)) {
        if(strlen($this->password) < 5 || strlen($this->password > 40)) {
            $this->error['password'] = 'Password length should be between 5 and 40 characters';
        }
    }
    if(!empty($this->userId) && !empty($this->newPassword)) {
        if(strlen($this->newPassword) < 5 || strlen($this->newPassword > 40)) {
            $this->error['password'] = 'Password length should be between 5 and 40 characters';
        }
    }
    if(!empty($this->pPhone)) {
        if(!preg_match('/^[0-9]{5,10}$/',$this->pPhone)) {
            $this->error['invalidpPhone'] = 'Invalid primary phone number';
        }
    }
    if(!empty($this->sPhone)) {
        if(!preg_match('/^[0-9]{5,10}$/',$this->sPhone)) {
            $this->error['invalidsPhone'] = 'Invalid secondary phone number';
        }
    }
    return (empty($this->error)) ? true : false;    
}

i have used lots of if conditions here which i think is not very good, is there any other way i could determine the above condition and rewrite the code in a more better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:46:04+00:00Added an answer on May 22, 2026 at 2:46 am

    You could use variable variables and loop through your code. But this means you’ll have to come up with some sort of standardized validation scheme for your code

    $validateFields = array('email', 'username', 'userId'); //list of fields to validate
    $rules = array(
      'username' => array('type'=> 'regex', 'rule' => '/^[a-zA-z ]{3,50}$/'),
      'email'    => array('type' => 'filter', 'rule' => 'FILTER_VALIDATE_EMAIL')
    );
    
    foreach ($validateFields as $field) {
       if (isset($rules[$field])) {
           switch ($rules[$field]['type']) {
               case 'regex' : 
                  if(!preg_match($rules[$field]['type']['rule'],$this->$field)) {
                        $this->error[$field] = ucfirst($field) . ' should be valid letters and should be between 3 and 25 characters';
                  }
                  break;
    
               case 'filter' :
                  if(!filter_var($this->$field, $rules[$field]['type']['rule'])) {
                      $this->error[$field] = 'Invalid email address';
                  }
                  break
    
               //more cases
           }
       }
    }
    return (empty($this->error)) ? true : false; 
    

    This example only uses one rule per field, but you should be able to easily extend it to use multiple rules per field.

    You’ll initially have to setup all the rules, but the validation won’t grow just because you add another property to your class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen many php scripts in which you can cascade class method to
Does PHP have a method of having auto-generated class variables? I think I've seen
I currently have a custom session handler class which simply builds on php's session
I have a variable $className which is name of declared class in php and
I have a PHP class that creates a PNG image on the fly and
I currently have my PHP class variables set up like this: class someThing {
I want to create a static class in PHP and have it behave like
Is there any way to have PHP automatically call a function, before a script
I have a problem while setting a private attribute on a PHP class, my
Below is part of a PHP database class someone else wrote, I have removed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.