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

  • Home
  • SEARCH
  • 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 6158445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:57:32+00:00 2026-05-23T20:57:32+00:00

I have a record class with 18 properties. Before that class can be submitted

  • 0

I have a record class with 18 properties.

Before that class can be submitted to the database, all 18 properties must have validated data.

Because I’m OOP-ifying a working procedural webapp, I went about this sort of backwards.

First I addressed workflow for modifying existing records. At the time, it made sense to throw all 18 properties into the __construct method and avoid craploads of setters. A separate loader class handles the dbase business and can return either single objects or an array of record objects. That all worked ok.

But then it came time to address the new record creation workflow, and suddenly I needed to instantiate an empty record, except my record constructor is a hungry beast that wants 18 parameters…

…so you strip the constructor? But then I’d have to add 18 setters and call them all each time I want to work with an existing record…

doesn’t seem like much of an improvement! :-/

How do real programmers handle this? (I’m just a weenie hobbyist…)

  • 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-23T20:57:32+00:00Added an answer on May 23, 2026 at 8:57 pm

    Either default arguments is one option, but then you have to fill out a large number of null’s if you only want to use, say, the first and last.

    Then again, you could do array looping:

    private $prop1;
    private $prop2;
    // more properties here.
    
    function __construct( array $props ) // `array` here is for type-hinting.
    {
        foreach( array( 'prop1', 'prop2' /*, all of the props for this object */
                 as $property )
        {
            // basically, this will assign all of the properties as they exist in the
            // props array
            if( isset( $props[ $property ] ) )
                $this->$property = $props[ $property ];
        }
    }
    

    Or, if you wanted to keep your old constructor signature:

    function __construct( $prop1, $prop2 = NULL, $prop3 = NULL /* ... */ ) 
    {
        if( is_array( $prop1 ) )
        {
             $this->array_prop_assignment( $prop1 );
        }
        else
        {
            $args = func_get_args();
            // this ensures that anything which is passed to the constructor
            // will go to the "new" old constructor
            call_user_func_array( array( $this, 'param_prop_assignment' ), $args );
        }
    }
    
    function param_prop_assignment( $prop1, $prop2 /* ... */ )
    {
        //your old constructor can go here.
    }
    
    function array_prop_assignment( array $props )
    {
        // foreach example above would go here.
    }
    

    The new version also gives you the option to simply:

    $k = new DataClass(); // no idea what the real class name is.
    $k->param_prop_assignment( 1, 2, 3 /* ... */ );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a standard active record model with an attributes that is required: class
Let's say that I have a record in the database and that both admin
I have a simple class that includes 2 properties, one String and one a
I have a business class that contains two nullable decimal properties. A third property
Lets say have this immutable record type: public class Record { public Record(int x,
I have a tree of active record objects, something like: class Part < ActiveRecord::Base
I have a record set that includes a date field, and want to determine
I cannot seem to google this one correctly... I have a class (Widget) that
i have a self join employees entity class with id,name and ref columns that
I have to store a TList of something that can easily be implemented as

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.