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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:21:27+00:00 2026-05-21T07:21:27+00:00

How to improve my attempt: class gotClass { protected $alpha; protected $beta; protected $gamma;

  • 0

How to improve my attempt:

class gotClass {
    protected $alpha;
    protected $beta;
    protected $gamma;
    (...)

    function __construct($arg1, $arg2, $arg3, $arg4) {
        $this->alpha = $arg1;
        $this->beta = $arg2;
        $this->gamma = $arg3;
        (...)
    }
}

to something nice and compact like (edited in response to comments)

class gotClass {
        protected $alpha;
        protected $beta;
        protected $gamma;
        (...)

    function __construct($alpha, $beta, $gamma) {
        $functionArguments = func_get_args();
        $className = get_called_class();
        $classAttributes = get_class_vars($className);
        foreach ($functionArguments as $arg => $value)
            if (array_key_exists($arg, $classAttributes))
                $this->$arg = $value;
}

I can’t get it work, I don’t know the right functions to use. Did I mention I’m new to PHP? Your help is much much appreciated.

EDIT: The field names do not conform to any pattern as the unedited post may have suggested. So their names cannot be constructed in some field[i]-like loop. My apologies for being unclear.

  • 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-21T07:21:28+00:00Added an answer on May 21, 2026 at 7:21 am

    Edit after comments: PHP’s lack of support for named arguments (that would fix this problem altogether) is a much-lamented thing. In my experience, as long as there is no native solution to the problem, it is indeed better to declare each parameter separately for the purposes of automatic document generation and the lookup function in your IDE. The IDE can show the expected parameters only if they are explicitly declared.

    See this SO question for another popular workaround to the issue.

    If you need to do this anyway, you need func_get_args() to retrieve all arguments passed to the constuctor, and an array to map the property names.

    Something like

    private $fields = array("alpha", "beta", "gamma");
    
    function __construct()
     { 
       $args = func_get_args();
       foreach ($args as $index => $arg)
        { $this->{($this->fields[index])} = $arg; }
     }
    

    this is not doing any checks on whether the specified variable exists – maybe add some more detail about what checks you exactly need.

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

Sidebar

Related Questions

I'm sure you can appreciate this attempt at laziness: I would like to improve
Just a question to improve my bash skills. I always do this: $ history
I am trying to improve my jQuery skill and I have this bit of
Basically I attempt to have a link slide down a div. This is fairly
I have recently started using the -Wall compiler switch in an attempt to improve
We use Visual Studio 2005 here, and in an attempt to improve the documentation
Sorry if this sounds simple, but I'm looking for some help to improve my
I have to write fast right-trimming function in C. My first attempt to do
I'm using Fluent NHibernate in an attempt to improve testability and maintainability on a
I am trying to follow: http://dev.mysql.com/doc/refman/4.1/en/fulltext-natural-language.html in an attempt to improve search queries, both

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.