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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:55:15+00:00 2026-05-17T02:55:15+00:00

This is probably pretty simple but I haven’t been able to figure out how

  • 0

This is probably pretty simple but I haven’t been able to figure out how to phrase the question for Google, so here goes:

So, I often do something like this…

class foo {
    private $bar = array();
}

… to set some class property to be an array. However, I’d like to make a private property an object instead, something like this:

class foo {
    private $bar = new stdClass();
}

I tried several variations of this and none of them worked. Can this be done? For bonus points can you assign a private property to be any other class of object?

Thanks!

  • 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-17T02:55:15+00:00Added an answer on May 17, 2026 at 2:55 am

    You can’t use functions (including constructors) in class member declarations. Instead set it up in the constructor for the class.

    class Foo {
    
      private $bar;
    
      private $baz;
    
      public function __construct() {
    
        $this->bar = new stdClass();
        $this->baz = new Bat();
    
      }
    
      public function __get($key) {
          if(isset($this->$key) {
            return $this->$key;
          }
    
          throw new Exception(sprintf('%s::%s cannot be accessed.', __CLASS__, $key));
      }
    
    }
    
    
    $foo = new Foo();
    
    var_dump($foo->bar);
    var_dump($foo->bat);
    

    And when you extend the class and need to override the constructor but still want the stuff in the parent classes constructor:

    class FooExtended
    {
       protected $coolBeans;
    
       public function __construct() {
    
          parent::__construct(); // calls the parents constructor
    
          $this->coolBeans = new stdClass();
       }
    }
    
    $foo = new FooExtended();
    
    var_dump($foo->bar);
    var_dump($foo->bat);
    var_dump($foo->coolBeans);
    

    It should also be noted this has nothing to do with visibility of the property… it doesn’t matter if its protected, private, or public.

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

Sidebar

Related Questions

This is probably a really simple jQuery question, but I couldn't answer it after
This probably has a simple answer, but I must not have had enough coffee
Ok, this probably has a really simple answer, but I've never tried to do
This is probably a silly question, but curiosity has gotten the better of me.
This is probably a really stupid newbie-sounding question to you developer type people, but
This probably sounds really stupid but I have noo idea how to implement jquery's
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
Which IoC container is the easiest to get started with. This probably equates to
This is probably best shown with an example. I have an enum with attributes:
This is probably explained more easily with an example. I'm trying to find a

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.