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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:37:43+00:00 2026-06-09T01:37:43+00:00

This is my first question, and something which has got me stumped. I’m not

  • 0

This is my first question, and something which has got me stumped. I’m not sure if this is something simple and I’m overlooking it or something not possible.

Below is a very simplified version of my original code. The end goal is to have the output as follows:

1: 
2: this is a test
3: this is another test
4: this is another test

However, with the code in its current state, its actual output is this:

1: 
2: this is a test
3: this is another test
4: 

I want object ‘B’ to be able to access the value of test_variable AFTER first_function() has altered it.

It works fine when I declare test_variable as static, however in the actual application it wouldn’t work and when I try to echo parent::test_variable it outputs ‘Object ID #17’ and so on.

class A
{
    public $test_variable;

    function __construct()
    {
        echo '1: ' . $this->test_variable . "<br />";
        $this->test_variable = 'this is a test';
        echo '2: ' . $this->test_variable . "<br />";
    }

    function first_function()
    {
        $this->test_variable = 'This is another test';
        echo '3: ' . $this->test_variable . "<br />";
        $b = new b;
        $b->second_function();
    }
}



class B extends A
{
    function __construct()
    {
        /* Dont call parent construct */
    }

    function second_function()
    {
        echo '4: ' . $this->test_variable;
    }
}

$a = new A;
$a->first_function();

// Outputs:
// 1:
// 2: this is a test
// 3: this is another test
// 4: 

// but I want it to output
// 1: 
// 2: this is a test
// 3: this is another test
// 4: this is another test

Many thanks for any responses. I greatly appreciate them.

Phil

  • 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-09T01:37:45+00:00Added an answer on June 9, 2026 at 1:37 am

    Declaring public $test_variable; inside the class means each instance (object) of the class has a copy. $test_variable in class A does not point to the same memory address as $test_variable in Class B. This is done intentionally to allow scope and remove a global state. As you said before, declaring it static will work, because then each instance shares the same variable.

    In this instance, $test_variable is, in essence, a dependency that class B requires. You can get that dependency via constructor injection fairly easily:

    class A
    {
        public $test_variable;
    
        function __construct()
        {
           echo '1: ' . $this->test_variable . "<br />";
           $this->test_variable = 'this is a test';
           echo '2: ' . $this->test_variable . "<br />";
        }
    
        function first_function()
        {
           $this->test_variable = 'This is another test';
           echo '3: ' . $this->test_variable . "<br />";
    
           // Instantiate instance passing dependency
           $b = new b($this->test_variable);
    
           $b->second_function();
        }
    }
    
    class B extends A
    {
        function __construct($dependency)
        {
           // Set dependency
           $this->test_variable = $dependency;
        }
    
        function second_function()
        {
           echo '4: ' . $this->test_variable;
        }
    }
    
    $a = new A;
    $a->first_function();
    

    So, that’s just a thought on how you might consider handling this.

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

Sidebar

Related Questions

It's possible that this question either has a simple answer that's a standard practice,
this is my first question to SO so i'll try not to disgrace myself.
this is my first question on stack overflow. Some quick background, this is not
Woo. My first question. I have a feeling I'm overlooking something pretty basic in
Two related questions. First: does this repro for you or is it something local
this is my first question in here, and I would like to ask if
This is my first question on Stack Overflow, so I hope that I'm clear
This is my first question, so please be gentle. I'm trying out Apache Camel
this is my first question I have searched for any launching activity questions and
this is my first question here so be gentle ! recently i try to

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.