DNB5brims
DNB5brims

Reputation: 30568

Recommend on how to set the php object more lazily?

Here is the way I do:

$aNewObject = new MyObj();
$aNewObject->set_id($row->id);
$aNewObject->set_user_id($row->user_id);
$aNewObject->set_title($row->title);
$aNewObject->set_url($row->url);
$aNewObject->set_description($row->description);                       
$aNewObject->set_status($row->status);

as you can see, I follow a name convention, which the object and the data base field is 100% match, I think there should have a way to help me to do it lazier, any recommendation?

Upvotes: 0

Views: 62

Answers (3)

fatnjazzy
fatnjazzy

Reputation: 6152

Lazier, it is not recommended, because it will make it hard to maintain by other programmers.

If you still want to do that, you should do with PHP reflection: http://www.php.net/manual/en/intro.reflection.php

or as @hakre answered.

Upvotes: 0

hakre
hakre

Reputation: 197684

You could do the setting dynamically by iterating over the fields (if that is meant by lazier):

$fields = array('user_id', ...);
foreach($fields as $field)
{
    $setter = "set_{$field}";
    $aNewObject->$setter($row->{$field});
}

It depends then where you want to place that code. Either just inline, as part of a function of MyObj (importRow($row)) or in a global helper function that always calls all setters matching object properties.

Within the class constructor:

$aNewObject = new MyObj($row);

class MyObj
{
    public function __construct($row = null)
    {
        ...
        $this->importRow($row);
    }

    public function importRow($row = null)
    {
        if (null === $row)
            return;

        foreach($row as $field => $value)
        {
            $setter = "set_{$field}";
            $this->$setter($value);
        }
    }
   ...    
}

To prevent duplicate code across different classes (missing traits support in PHP < 5.4), a global static function or object can do it:

$aNewObject = new MyObj();
new Setter($aNewObject, $row);

# or

$aNewObject = Setter::fill('MyObj', $row);


class Setter
{
    private $object;
    public function __construct($class, $data)
    {
        // works on classnames or objects
        if (is_string($class))
            $object = new $class();
        else
            $object = $class;

        $this->object = $this->import($object, $data);
    }

    private function import($object, $data)
    {
        foreach($data as $field => $value)
        {
            $setter = "set_{$field}";
            $object->$setter($value);
        }
        return $object;
    }

    public function getObject()
    {
        return $this->object;
    }

    public static function fill($class, $data)
    {
        $self = new __CLASS__($class, $data);
        return $self->getObject();
    }
}

Upvotes: 1

Dan Grossman
Dan Grossman

Reputation: 52372

You can get even lazier by only writing

$aNewObject = new MyObj($row);

and having a constructor that sets the object's properties based on the contents of $row

Upvotes: 3

Related Questions