Jump to content

Flexible class loading


Jonob

Recommended Posts

Lets say that I have an array that I want to convert to a value object

My value object class is as follows:

 

/* file UserVO.php*/
class UserVO
{
    public $id;
    public $email;

     public function __construct($data)
     {
         $this->id = (int)$data['id'];
         $this->email = $data['email'];
     } 
}

 

And I create my array of value objects as follows:

/* file UserService.php*/
$array = array(
array(...),
array(...));
$count = count($array);
for ($i = 0; $i < $count; $i++)
{
   $result[] = new UserVO($array[$i]);
}
return $result;

 

OK, so this all works fine. However, I'd like to specificy the VO that is to be created dynamically, so that I can have a single dynamic function to create my VO's.

 

Something like:

$ret = create_vo($array, 'UserVO');

function create_vo($data, $vo)
{
  $count = count($data);
  for ($i = 0; $i < $count; $i++)
  {
     $result[] = new $vo($array[$i]); //this obviously wont work...Class name must be a valid object or a string
  }
  return $result;
}

 

I realise that I could do this with a switch statement (iterating through all my VO's)...but there is no doubt a much much more elegant solution. It would also be supercool if I could lazy load the VO's as needed, instead of having multiple 'includes'

 

Any help much appreciated.

Link to comment
Share on other sites

You clearly misunderstood the use of Value Object's. A Value Object does not contain an identity. Value Object's are to keep your domain model simple while still being able to fully encapsulate your model (and not breaking the client contract in the future).

 

Eric Evan defines a Value Object as:

 

When you care only about the attributes of an element of the model, classify it as a Value Object. Make it express the meaning of the attributes it conveys and give it related functionality. Treat the Value Object as immutable. Don't give it any identity and avoid the design complexities necessary to maintain Entities.
Link to comment
Share on other sites

I'm not an OOP wizard so I'll leave that to ignace, but your code works fine for me except that:

$result[] = new $vo($array[$i]);

 

Should be:

$result[] = new $vo($data[$i]);

 

And I would foreach it as it's easier:

function create_vo($data, $vo)
{
foreach($data as $arg)
  {
     $result[] = new $vo($arg);
  }
  return $result;
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.