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 363305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:19:47+00:00 2026-05-12T13:19:47+00:00

What is the best way of creating and populating an Object from values passed

  • 0

What is the best way of creating and populating an Object from values passed in from a form?

If for example, i have a class Car with properties Colour, Model, Make, Year and a method Save, which will either insert or update the record.

I then have a form that has fields for all these values and it is submitted. I want to create an instance of class Car with the posted values and then call the Save method. All good and well.

But what is the best way of assigning the posted values to the objects internal properties. Assuming this is a simplified scenario and the actual situation would have many more properties, making individual Set calls long-winded.

Is it best to simply call the Set method for each one? Or pass in an array to a method (or the constructor) which then calls the Set methods? Or some other way?

Any advice on best practices is appreciated

Cheers
Stuart

  • 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-12T13:19:47+00:00Added an answer on May 12, 2026 at 1:19 pm

    I would implement the magic function __set_state(), as it is intended for exactly this use case. There are multiple benefits of putting the implementation into that method:

    1. It is very well defined, what this function does (It is defined in the PHP docs, in contrast to your own source code)
    2. You can access private members within the function
    3. Objects dumped with var_export() will be automatically reconstructed using this function.

    EDIT As requested in the comments:

    class A {
        private $member = 1000;
        public static function test(A $a) {
            echo $a->member;
        }
    }
    echo A::test(new A()); // outputs 1000
    

    EDIT Fulfilling another request from the comments:

    You cannot know on which class a static method was called unless you are using php 5.3, in which the required feature (late static binding) was introduced. What you can do in emulating get_called_class() by analyzing the current stack (using debug_backtrace()). If you have a working get_called_class() function, you can do the following:

    class BaseClass {
        public static function __set_state($state) {
            $class = get_called_class();
            // Assumption: Constructor can be invoked without parameters!
            $object = new $class();
            foreach (get_class_vars($class) as $member) {
                if (!isset($state[$member])) {
                    // Member was not provided, you can choose to ignore this case
                    throw new Exception("$class::$member was not provided");
                } else {
                    // Set member directly
                    // Assumption: members are all public or protected
                    $object->$member = $state[$member];
                    // Or use the setter-approach given by Chaos.
                    $setter = 'set' . ucfirst($member);
                    $object->setter($state[$member]);
                }
            }
            return $object;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling with the best way of creating, accessing and updating values
Just wondering what is the best practice way of creating a class that needs
The best way to explain my problem is by a example. I have a
Do anyone have an idea whats the best way of creating an own library
What's the best way to pull data from Wordpress database? I think creating a
What is the best way of creating multiline plot. Is it possible to have
I have been in this forum before to find the best way of creating
What is the best way of creating an array of ember objects from an
whats the best way of creating a 'const char*' using available variables? For example,
what is the best way for creating a jsp visitor hit counters? is it

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.