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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:01:46+00:00 2026-06-18T00:01:46+00:00

I have a single instance parent class, which holds ‘local global properties’, that every

  • 0

I have a single instance parent class, which holds ‘local global properties’, that every child class should have access to (also when it is changed)

I can think of two ways to make sure a child from an instantiated parent inherits the parent values:

1) Using static variables

class p
{
public static $var = 0;
}

class c extends p
{
function foo()
{
echo parent::$var;
}
}

$p = new p;
$c = new c;

$c->foo(); //echoes 0
p::$var = 2;
$c->foo(); //echoes 2

2) Using passed object

    class p
    {
          public $var = 0;
    }

    class c extends p
    {
           function update_var(p $p)
      {
            $this->var = $p->var;
      }

    function foo()
    {
    echo $this->var;
    }
    }

    $p = new p;
    $c = new c;

    $c->foo(); //echoes 0
    $p->var = 2;
$c->update_var($p);
    $c->foo(); //echoes 2

In my opinion the static solution is by far the most elegant one, but there might be some drawbacks, that I’m not seeing. Which solution do you believe to be the best, or is there a third better option?

(Also note, in this example the $var is public to make this example easier to illustrate, but will end up protected)

  • 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-18T00:01:47+00:00Added an answer on June 18, 2026 at 12:01 am

    that every child class should have access to (also when it is changed)

    This means that you want to use static variables, since this is exactly what they were designed for.

    Another option would be to use constants, but if they fit neatly in your class, that static is the way to go

    Edit: Or, you could use the pattern @true suggested if you need something a bit more complex.

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

Sidebar

Related Questions

I have a service that I would like it to become single instance, because
I have a data storage requirement which is an excellent candidate for single instance
I have single game instance class in my study rock-scissors-paper game on C++. I
I have a Singleton class to have only one single instance of my MPIProxy
We have an issue where on a single instance of our product we receive
Is it possible to have several consumers listening on a single MSMQ instance and
i have a single filed which i need to clone with a clone button
I have A single page that has the following structure <form runat=server><placeholder></placeholder></form> I have
I have a single-view application that takes in a username and a password and
I have a single page website that changes content based on variables passed through

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.