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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:02:19+00:00 2026-06-04T17:02:19+00:00

Is it possible to initialize an objects private or protected members in php with

  • 0

Is it possible to initialize an objects private or protected members in php with an associative array.

for example:

    class TestClass
{
    public $_name;
    public $_age;


    public function __construct(array $params)
    {
        ??????
    }
}


$testClass = new TestClass(
    array(
        'name'  => 'Bob',
        'age' => '29',
    )
);

i was wondering whether there is an elegant solution – perhaps by implementing one the spl interfaces or otherwise?

  • 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-06-04T17:02:22+00:00Added an answer on June 4, 2026 at 5:02 pm

    You mentioned SPL. But without knowing the exact requirements for the purpose of your object, the below is about the only information I can give…

    You could have your object extend the SPL built-in class ArrayIterator. Then, without concern for handling it in the constructor (already handled in the parent ArrayIterator class), you could import an array into your object simply like so:

    class testClass extends ArrayIterator
    {
    
     /* child '__construct' method not required */
    
     /* rest of your code here */
    
    }
    
    $t = new testClass(array('name' => 'asdf', 'age' => 99));
    

    Keep in mind that with default ArrayIterator behavior, you cannot later access any of the passed array values as you would with a normal object property. You must access them as you would an array:

    echo $t['name']; // 'asdf'
    echo $t->name; // NULL property unknown error
    

    And, internally, the passed array is stored within your object as a single private storage parameter. In your case, you already have all your object properties pre-defined and prepended with an underscore, so you would probably have to manually loop over $this or $params anyway in your constructor to set any real object properties.

    You could of course redefine all your child object’s ArrayIterator inherited methods to handle your special property naming case on get or set, but this would seem redundant and unproductive as opposed to just looping over $params/setting $this anyway in your constructor.

        public function __construct(array $params)
        {
            foreach ($params as $key => $val) {
                if (property_exists($this, "_$key")) {
                    $this->{"_$key"} = $val;
                }
            }
        }
    

    So, just looping over $params/setting $this within your constructor is probably the best, most simple solution there is.

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

Sidebar

Related Questions

Is it possible to initialize objects of one class in the constructor of another
In C++, it's not possible to initialize array members in the initialization list, thus
I wonder if it is possible to initialize an entire array with a constexpr
Is it possible to declare class variable outside of if-else and initialize it inside
Is it possible to do the following (e.g. initialize bool array and set all
I've got the following setup: public class SomeClass { private DirectoryEntry _root; private DirectorySearcher
A java class does something like the following public class Foo { private final
I have the class: class SomeClass<T extends SomeInterface>{ private T[] myArray; public SomeClass() {
Why is it possible to use an object initializer to set a private set
Why is not possible to initialize a property to a function when you declare

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.