Object Oriented PHP Best Practices

Posted by user270797 on Stack Overflow See other posts from Stack Overflow or by user270797
Published on 2010-03-09T09:14:58Z Indexed on 2010/03/09 9:21 UTC
Read the original article Hit count: 899

Filed under:
|
|

Say I have a class which represents a person, a variable within that class would be $name.

Previously, In my scripts I would create an instance of the object then set the name by just using:

$object->name = "x";

However, I was told this was not best practice? That I should have a function set_name() or something similar like this:

function set_name($name)
{
    $this->name=$name;
}

is this correct?

If in this example I want to insert a new "person" record into the db, how do I pass all the information about the person ie $name, $age, $address, $phone etc to the class in order to insert it, should I do:

function set($data)
{
    $this->name= $data['name'];
    $this->age = $data['age'];
    etc
    etc

}

Then send it an array? Would this be best practice? or could someone please recommend best practice?

© Stack Overflow or respective owner

Related posts about php

Related posts about oop