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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:41:27+00:00 2026-05-15T14:41:27+00:00

Hey, everyone. I couldn’t find anything Googling this problem, and I’ve found really good

  • 0

Hey, everyone. I couldn’t find anything Googling this problem, and I’ve found really good answers to some questions on SO before, so I’m taking this excuse to join the community.

I’m creating a hierarchy of classes for a PHP project I’m working on, and I’d like to have some variables in the classes initialized within the constructor function without explicitly writing the initialization code. Specifically I want to have the interpreter assume that some of the variables are actually pointers to a certain class. If I could do something like C structs, that would be pretty close to what I want.

So far, the only thing I came up with is to explicitly state the variable type within its own name and have the class call an initializing function on each, like;

class A{
  ...
}

class B{
  var $x_A;

  function initVar($var){
    list($varname, $vartype) = split('_',$var);
    $this->$var = new $vartype();
  }

}

And B’s constructor calls initVar on all of its get_class_vars(get_class($this)), so anything that inherits from it will do initialization in the same way; obviously including a check on the variable name, a check that the class exists, and a better separation scheme than a single underscore. I just cannot help but think that there is a better way to do this that isn’t hardcoding the initialization into the construction function.

If anyone knows of a better way to do this, your help would be much appreciated.

  • 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-15T14:41:27+00:00Added an answer on May 15, 2026 at 2:41 pm

    Your method there would probably work, however it could get very tiresome to actually use it. For example, imagine having to reference those variables all the time:

    $myObject->account_ServiceAccountType
    

    If you really wanted to go with something like this, perhaps a mapping variable might be useful:

    class B {
        public $x, $account;
    
        private $map = array(
            'x' => 'A',
            'account' => 'ServiceAccountType'
        );
    
        public function __construct() {
            foreach ($this->map as $var => $class) {
                $this->$var = new $class;
            }
        }
    }
    

    You could also try this method out. It uses PHP’s magic __get function.

    class B {
        private $map = array(
            'x' => 'A',
            'account' => 'ServiceAccountType'
        );
        private $vars;
        public function __construct() {
            $this->vars = array();
            foreach ($this->map as $var => $class) {
                $this->vars[$var] = new $class;
            }
        }
    
        public function __get($v) {
            return $this->vars[$v];
        }
    }
    

    Notice that you don’t need to define the class members twice using this method.

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

Sidebar

Related Questions

Hey everyone, this seems like it should be a simple one; I really hope
Hey everyone. I'm really struggling to figure out the logic with this one and
Hey everyone I've looked for hours trying to find a solution to this, my
Hey everyone. I am having some issues on my end and hopefully it's just
Hey everyone, great community you got here. I'm an Electrical Engineer doing some programming
Hey everyone, this is my first post up on StackOverflow! WOO! Anyhow, onto my
Hey everyone. While I'm trying to learn some PHP and mySQL, I ran into
Hey everyone! I am new to blackberry programming although I do have some java
Hey everyone, I've been having some trouble in C, i'm using a variety of
Hey Everyone. I'm a first time poster, but I've browsed this site a number

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.