Reputation: 131
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.
Upvotes: 1
Views: 309
Reputation: 928
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 :)
Upvotes: 0
Reputation: 174947
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.
Upvotes: 3