I have a user class comprised of the code below. It has functions to insert a user, update a user, delete a user, look up a user, and search for users. What I'm wondering is if I this class would pass the Single Responsibility Principle as stated here (http://en.wikipedia.org/wiki/Single_responsibility_principle).

class user{
    $first_name = "";
    $last_name = "";
    $physical_address1 = "";
    $physical_address2 = "";
    $physical_city = "";
    $physical_state = "";
    $physical_zip = "";
    $mailing_address1 = "";
    $mailing_address2 = "";
    $mailing_city = "";
    $mailing_state = "";
    $mailing_zip = "";
    function __construct($array){
        if(!isset($array['first_name']) && !empty($array['first_name'])){
            $this->first_name = "";
        if(!isset($array['last_name']) && !empty($array['last_name'])){
            $this->last_name = "";
        if(!isset($array['physical_address1']) && !empty($array['physical_address1'])){
        if(!isset($array['mailing_address1']) && !empty($array['mailing_address1'])){
    function set_physicalAddress($array){}
    function get_physicalAddress(){}
    function set_mailingAddress($array){}
    function get_mailingAddress(){}
    static function insert($array){}        
    static function update($array){}
    static function delete($int){}
    static function lookup($array){}
    static function search($array){}

$obj = new user(array(

The purpose of having the constructor parameter value be an array is that I feed it only the values I want without having to deal with errors caused by not setting a parameter. Instead of setting the parameters to null it's easier to just not put them in the array.

  • \$\begingroup\$ Can you post an example usage of this class? I'm not quite sure I understand how you're using it. Why does the constructor accept an array? What is in the array the constructor accept? And are you modeling your users with arrays? \$\endgroup\$
    – Corbin
    May 16, 2012 at 18:07

1 Answer 1


I don't think the user class you provided satisfies the Single Responsibility Principle.

I can see a few situations that may require changes to the user class.

  1. You want to change what information about the user is stored. This could happen if you need to allow a title (Mr., Mrs., etc.) and/or a middle name. Doing so would require a change to the user class.

  2. You want to create a new class that also contains address information. The new class would likely duplicate code that is already present in the user class.

  3. You want include the user's country with the physical and mailing addresses. Doing so would require a change to the user class. It would also require changes to the class in #2.

  4. You want to change where the information is stored. You may decide you want to be able to test your class without needing to connect to the database (assuming you're connecting to a database). Doing so would require a change to the CRUD methods (insert, update, delete, lookup, and search) of your class.

(1) is related to the user class and changing the class is expected. No problem here.

(2), (3), and (4) are really not the responsibility of the user class, yet they would probably require changes to the user class.

You should consider creating an additional "address" class that can be reused for the physical and mailing addresses for the user. The user class could contain instances of the address class, and would not require changes if the address functionality changed. Any new classes that need address information could reuse this class.

You should also consider creating a new class (let's call it userProvider) that is responsible for the CRUD functions. This class could have the CRUD methods with a user object as a parameter. Your user class could still define the CRUD methods if you want, but would simply pass the user instance to the userProvider class, which would be responsible for storing/retrieving the user information.

  • \$\begingroup\$ Ah, my class allows for all parts of a name prefix through suffix. I just didn't want to include all of the code. I hadn't noticed the duplicity of locations. Thank you for pointing that out. I do in fact have a class for locations. I like the idea of a userProvider class it would solve a lot of my issues and allow for more usability. Though wouldn't these changes and others to mirror these changes to all other lead to over classation for lack of a better term. \$\endgroup\$ May 17, 2012 at 2:47
  • \$\begingroup\$ @Brook Yes, implementing these changes will likely lead to more classes. The changes may or may not be appropriate for your project. If this is a small one-off project, you may not need to worry about it. If this is a project that could grow beyond your current expectations, the additional separation may be helpful. I don't know the details of your project. I was simply answering your question of whether or not the example class you provided met the Single Responsibility Principle. It is up to you to decide whether or not the changes are really neccessary, based on your project. \$\endgroup\$
    – tgriffin
    May 17, 2012 at 4:25
  • \$\begingroup\$ Thank you for your detailed explanation. You have been a great help to me. \$\endgroup\$ May 17, 2012 at 11:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.