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

What I want to do is following. Lets say we have array like that
So lets say I have an array that I want to organize by the
Lets say that i have the following array : float QuadVertices[4 * 2]; float
Lets say I have an array numbers that contains the following values: int numbers
Ok so lets say I have an array that can be 0 - X
Lets say that i have 2 pages: index.php and service.php index.php sends an http-post
Lets say that you have a business object whose current state implies that there
Lets say that I have an executable and when it is started I want
Lets say I have an array X that contains [A,B,C,D,nil]; and I have a
Lets say I have the following two PHP arrays that contain integers: $foo =

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.