Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1077781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:37:36+00:00 2026-05-16T21:37:36+00:00

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

  • 0

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($data[$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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:37:37+00:00Added an answer on May 16, 2026 at 9:37 pm
    $result[] = new $vo($data[$i]); //this obviously wont work...Class name must be a valid object or a string
    

    Have you tried it? It works just as expected (in php 5.1, I don’t know how was it with OOP in php 4, but if you are using __construct for constructor this should work).

    To avoid multiple includes define magic function __autoload before using any class

    function __autoload($className)
    {
        require_once 'myclasses/'.$className.'.php';
    }
    

    So first call new UserVo will trigger this function to include file myclasses/UserVo.php.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that you have websites www.xyz.com and www.abc.com. Lets say that a user
Lets say that I have a header user control in a master page, and
Lets say you have interface definition. That interface can be Operation . Then you
I have one field that I need to sum lets say named items However
Using Rails v2.1, lets say you have an action for a controller that is
Let's say I have a fairly simple app that lets users store information on
Let's say that you have overridden an object's equals() and hashCode() methods, so that
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I have a widget that displays summary information about how many
Let's say that you have a product that is written in Visual Studio and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.