1

this is the first time I try OOP, and I'm not skilled in English so sorry for errors.

I want to manage a list of people in my database with a lot of attributes, I have to extract all the records or save single one. I'm trying to create a class with all the attributes but I don't know how to initialize the single object because I think creating a constructor with so many parameters is not the correct way. I need a suggestion please. Thanks a lot.

Francesco

EDIT: an example of what I should do.

class Person {
    var $id=NULL;
    var $name;
    var $lastname;
    var $cf;
    var $address;
    var $number;
    var $city;
    var $cap;
    var $state;
    var $nation;
    var $phone;
    var $fax;
    var $cell;
    var $email;
    var $reg_date;
    var $reg_type;

    function __constructor(... attributes ...){
        ... assignements ...
    }

    function getPerson($id){
        $result=mysql_query("SELECT * FROM .... ");
        if(mysql_num_rows($result)!=1) return false;
        $record = mysql_fetch_assoc($result);
        $this->id=$record['id'];
        $this->name=$record['name'];
        ecc ...
    }
}

I need to create a Person object with the data sent by a web form in POST mode.

1
  • A constructor could probably handle a million "attributes". Whether it makes sense or not is another matter. So, how many "attributes" are we talking about here, and why do you think that it is "not the correct way"? If you can show us some code where you try to implement this then it will be easier for us to answer your question. Commented Jul 29, 2013 at 18:20

2 Answers 2

3

A better way could be by using a mapper.

You have your Person Object, and you have a PersonMapper object, which is only responsible of mapping what's in the database to the person, and vice versa.

So...

class Person {
    //All attributes here

    //Setter and getter methods as necessary
}

class PersonMapper {
    public function fetch(Person $person)
    {
         //Contact the database and fetch all the data into $person;
    }
    //Mapping from the database
}

So that you use it like so:

$person = new Person;
$mapper = new PersonMapper;

$mapper->fetch($person); //$person should now hold all of the data in its properties.

This gives you the advantage of not being tied to a single data source, it can be a database, a file, a remote API call, or whatever.

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

2 Comments

OK, but in this case I can insert the metod directly in the class person (or not?). I'm thinking that the problem is not the fetching of the data, because I can do it with a method like fetch($id) where $id is the value of the primary key attribute of my people's table. I've a form for the registration of the single person, how can I create an object with the data passed through the $_POST variable?
Note that PHP is relatively slow at invoking objects. So this methodology can be expensive if a single request might result in hundreds more class instances.
0

You are on the right path, just use PHP5 OOP, PHP4 style is out of date for some time now, plus PHP5 will give you more function you could use here.

Like:

class Person {
    public $id=NULL;
    public $name;
    public $lastname;
    public $cf;
    public $address;
    public $number;
    public $city;
    public $cap;
    public $state;
    public $nation;
    public $phone;
    public $fax;
    public $cell;
    public $email;
    public $reg_date;
    public $reg_type;
}

You can set those properties by passing an object or array in constructor, when instantiating object. And you can set/get value from them direct because they are public, like:

$person_obj = new Person($data);

//set name
$person_obj->name = 'Joe';

//get name
echo $person_obj->name;

You can still use protected and private properties and methods wherever you need them to do any operation on your person data, and yet whenever you pass this object to database or some other source only public properties will be seen and may be used, protected and private properties and all the methods remain intact.

In a way you may consider object and is public properties as associative array. And as a plus using OOP you will get all the extended functionality in one class with all the methods and protected/private properties you may think of.

Hope this makes sense :)

2 Comments

Well, so I can pass the entire $_POST array in the method for the assignement and then save data to database with another one. Now I've another question: I want to populate a series of different html table with all the people from my database, should I create an array of objects and then loop them? @BenDeMott says that this operation can be expensive with hundreds of records.
Yes, array of objects and then loop them, it may be more costly resource wise than array, but not that much that it would really matter. For creating this array of user objects consider the factory patern. You can google for it, there is plenty of info online.

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.