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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:36:05+00:00 2026-05-11T01:36:05+00:00

I have 2 classes, main and extended. I need to use main vars in

  • 0

I have 2 classes, main and extended. I need to use main vars in extended class.

<?php class Main {   public $vars = array(); }  $main = new Main;  $main->vars['key'] = 'value';  class Extended extends Main { }  $other = new Extended;  var_dump($other->vars);  ?> 

Who I can do it?

No valid for example:

<?php class Extended extends Main {   function __construct ($main) {     foreach ($main as $k => $v) {       $this->$k = $v;     }   } } ?> 

I need some solution more transparent and efficient 🙂

  • 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. 2026-05-11T01:36:05+00:00Added an answer on May 11, 2026 at 1:36 am

    EDIT: This can be solved much better with Inversion of Control (IoC) and Dependency Injection (DI). If you use your own framework or one without Dependency Injection Container try League/Container

    Answer below left as history of foolish answers.


    The correct way I figure.

    <?php class Config {     protected $_vars = array();      protected static $_instance;      private function __construct() {}      public static function getInstance()     {         if (!isset(self::$_instance)) {             self::$_instance = new self();         }         return self::$_instance;     }      public function &__get($name) {         return $this->_vars[$name];     }      public function __set ($name, $value) {         $this->_vars[$name] = $value;     } }  $config = Config::getInstance(); $config->db = array('localhost', 'root', ''); $config->templates = array(     'main' => 'main',     'news' => 'news_list' );  class DB {     public $db;      public function __construct($db)     {         $this->db = $db;     }      public function connect()     {         mysql_connect($this->db[0], $this->db[1], $this->db[2]);     } }  $config = Config::getInstance(); $db = new DB($config->db); $db->connect();  class Templates {     public $templates;      public function __construct($templates)     {         $this->templates = $templates;     }      public function load ($where) {         return $this->templates[$where];     } }  $config = Config::getInstance(); $templates = new Templates($config->templates); echo $templates->load('main') . '\n'; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some classes layed out like this class A { public virtual void
I have two classes public class A { public A() { } } public
I'm about to learn the java threading facility. I have 2 classes: public class
I have a file called main.py and a file called classes.py main.py contains the
I have two classes, and want to include a static instance of one class
I have multiple classes that all derive from a base class, now some of
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes that each need an instance of each other to function.
I have: Main Program Class - uses Library A Library A - has partial
I have seen this: <classpathentry excluding=** kind=src output=target/classes path=src/main/resources/> In my Eclipse .classpath file

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.