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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:24:03+00:00 2026-05-12T12:24:03+00:00

please tell me theres a way around this… $my_var = ‘hello’; class Test{ private

  • 0

please tell me theres a way around this…

$my_var = 'hello';

class Test{

    private $my_var;
    private $my_internal_var = 'if you see this it works!';

    function __construct(){
        global $my_var;

        $this->my_var = &$my_var;
    }

    function get_my_var(){
        return $this->my_var;
    }

    function set_my_var($value){
        $this->my_var = $value;
    }

    function set_my_var_to_internal_reference(){
        //this line should make $my_var in root, $my_var in this object, and $my_var in
        //all external functions point to $my_internal_var.
        $this->my_var = &$this->my_internal_var;
    }
}


function get_my_var(){
    global $my_var;

    return $my_var;
}

function set_my_var($value){
    global $my_var;

    $my_var = $value;
}


$my_obj = new Test();

echo '<h2>set default starting value</h2>';
echo 'obj : '.$my_obj->get_my_var().'<br>'; //echoes 'hello' - this is ok
echo 'func: '.get_my_var().'<br>'; //echoes 'hello' - this is ok
echo 'root: '.$my_var.'<br>'; //echoes 'hello' - this is ok

set_my_var('hello world!');

echo '<h2>set to value by function</h2>';
echo 'obj : '.$my_obj->get_my_var().'<br>'; //echoes 'hello world!' - this is ok
echo 'func: '.get_my_var().'<br>'; //echoes 'hello world!' - this is ok
echo 'root: '.$my_var.'<br>'; //echoes 'hello world!' - this is ok

$my_obj->set_my_var('hello world again!');

echo '<h2>set to value by object method</h2>';
echo 'obj : '.$my_obj->get_my_var().'<br>'; //echoes 'hello world again!' - this is ok
echo 'func: '.get_my_var().'<br>'; //echoes 'hello world again!' - this is ok
echo 'root: '.$my_var.'<br>'; //echoes 'hello world again!' - this is ok

$my_obj->set_my_var_to_internal_reference();

echo '<h2>set to object internal reference</h2>';
echo 'obj : '.$my_obj->get_my_var().'<br>'; //echoes '      ' - this is NOT ok. should be 'if you see this it works!'
echo 'func: '.get_my_var().'<br>'; //echoes 'hello world again!' - this is NOT ok. should be 'if you see this it works!'
echo 'root: '.$my_var.'<br>'; //echoes 'hello world again!' - this is NOT ok. should be 'if you see this it works!'

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-12T12:24:03+00:00Added an answer on May 12, 2026 at 12:24 pm

    This is NOT ok as is. You should use a wrapper object in OO enviorment, and don’t use global vars, they are sucks. Example:

    class My_Registry{
        private $registry = array();
    
        public function set_var($key, $var){
            $this->registry[$key] = $var;
            return $this;
        }
    
        public function get_var($key){
           if(isset($this->registry[$key]){
               return $this->registry[$key]
           }else{
               return false;
           }
        }
    }
    

    Or something like this, you can add error handling, privileges, etc.

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

Sidebar

Related Questions

please tell me where is leak in this code... //here I did video with
Okay, I really know this has GOT to be the long way around doing
I'm hell bent on making this work with NAudio, so please tell me if
I am new in iphone. Please tell me is there any way to integrate
Any of you gone through this task? Please tell me a solution. I have
Could you please tell me if there is any equivalent of Access' DISTINCTROW for
Can anyone please tell me is there any special requirement to use either EXTERN
Please tell me what is the Qt equivalent function for glutswapbuffers()..
Please tell me why my window doesn't render. Below is the javascript that i
Please tell me if it is possible to do the following: create an instance

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.