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

  • SEARCH
  • Home
  • 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 4243846
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:37:50+00:00 2026-05-21T03:37:50+00:00

My brain is starting to hurt so I decided I’d ask here. I have

  • 0

My brain is starting to hurt so I decided I’d ask here.

I have a data object, Employee. Getters, setters, formatters, etc. I have a manager, EmployeeManager, which handles database access and other things. Right now I have a large validation block in EmployeeManager, but I’ve been wondering if I could move some of that to the setters.

For example, right now I have;

public function getSSN($bFormatted = true) { 
    return ($bFormatted) ? $this->format_ssn($this->SSN) : $this->SSN; 
}
public function setSSN($s, $bValidate = false)
{
    // If we're validating user entry, save a copy.
    // Either way, store a trimmed version.
    if ($bValidate): $this->SSNToValidate = $s; endif;
    $this->SSN = str_replace('-', '', $s);
}
public function getSSNToValidate() { return $this->SSNToValidate; }

What this does is:
* When you set an SSN, if it’s being done by the system (like from the database) then it does setSSN('123456789', false), because SSNs are stored in the database sans dashes.
* When you set an SSN from user input, it does simply setSSN('123-45-6789') then not only trims the dashes, but also stores a raw version to validate (because I want to validate based on format)
* When you get an SSN, if formatting is requested (and it always is except when you’re writing to the database), it formats it based on another function in the Employee class.

So my question is: Could I perhaps add the validation to the setter here, instead of relying on the monolithic validate function in the Manager class? Because I’m starting to have to deal with errors coming from all over the application, I’ve decided for the moment to move to a central Error Handler static class, rather than each manager maintain its own list of errors.

And because of this, I could easily add error handling to this:

public function setSSN($s, $bFromUser = false)
{
    if ($bFromUser && !$this->validateSSN($s))
    {
        ErrorHandler::add(array('ssn' => 'Invalid SSN entered')); 
    }
    else
    {
        $this->SSN = str_replace('-', '', $s);
    }
}

So I guess my question is: Does this make sense at all or am I hosing myself by moving validation from the manager (to be performed on demand or just before writing to the database) to the object (to be performed on entry)?

This is overall generic, I’m just using SSN as a good example.

  • 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-21T03:37:51+00:00Added an answer on May 21, 2026 at 3:37 am

    You should always validate when you’re creating an object or setting values in the object. This way you are always guaranteed to have an object that is in a valid state. If the validation is not in the object itself you can not guarantee that the object will ever be validated.

    Example validation outside of the object:

    In this case there is a bug and we forgot to validate SSN.

    class EmployeeManager
    {
        function saveEmployee($employee)
        {
            //Oops, we forgot to validate SSN
            $db->save($employee);  //This is just SQL to persist the employee.
        }
    }
    
    //Somewhere else in code...
    $employee = new Employee();
    $employee->setSSN("FredFlintstone");  //No validation is done here.
    $employeeManager = new EmployeeManager();
    $employeeManager->saveEmployee($employee);  //This call will persist FredFlintstone because validation was missed.
    

    Example validation inside of the object:

    In this case the employee object validates all of it’s input. This way we know that if we have a instance of an employee all the data within it is valid.

    class Employee
    {
        function setSSN($input)
        {
            if(strlen($input) != 9)
            {
                throw new Exception('Invalid SSN.');
            }
            //Other validations...
            $this->ssn = $input;
        }
    }
    
    //Somewhere else in code...
    $employee = new Employee();
    $employee->setSSN("FredFlintstone");  //This call will now throw an exception and prevent us from having a bad object.
    $employeeManager = new EmployeeManager();
    $employeeManager->saveEmployee($employee);
    

    In addition you should never allow an object to be created that is not fully initialized. If say a SSN is required for an employee then don’t supply an empty constructor. Your constructors should have parameters for all required fields (unlike my example which I omitted them for clarity).

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

Sidebar

Related Questions

My brain is fried at the moment, so here's the scenario. I have a
Ok, I'm really starting to have a bad day and the ol' brain isn't
Hey Im totally out of my depth and my brain is starting to hurt..
This question burns my brain. I have an object on a plane, but for
I just cannot work this out. My god it's making my brain hurt, so
I have tried, and burned my brain to understand the definition of Regular Languages
Brain squeeze and need help, I have a table with 6 rows that are
EDIT I just realized that I must have had a massive brain fart while
I'm wrapping my brain around mobile sites using jquerymobile's framework. So far I have
My brain isn't working. Please, let yours work for mine for 1 minute. The

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.