0

Is it possible to have a PHP class and set up a variable inside of the class in such a way that it can be used without referring to the class first. In other words, can such a variable somehow be made a global? Or if not a global, what code could make a variable outside a class refer to the one inside the class.

Class Example:

class A {
    $my_var = '';
}

Code Example:

$new_a = new A();
$my_var = 'some string'; 
//This should change the variable inside the class, 
//without having to call $new_a->my_var = 'some string';

Is this in any way possible?

Thank you Michael

6
  • 1
    How could php know you mean the variable inside the class? Commented Feb 22, 2012 at 13:07
  • Global state is very very very bad. If you can do what you want in PHP then I really don't want to know how! Commented Feb 22, 2012 at 13:08
  • @damien-pirsy That is exactly what I want to know. What code would make php understand that i am referring to the variable inside the class. Commented Feb 22, 2012 at 13:08
  • 1
    @MichaelFrey that would be $new_a -> my_var Commented Feb 22, 2012 at 13:11
  • 1
    As with many questions here on SO, my question after reading this one is: why on earth would you want that. Please explain some of the context so people can help with your original problem, since this is a solution you came up with that causes more problems. Let us help solve the original problem, not this crazy one. Commented Feb 22, 2012 at 13:13

1 Answer 1

1

No. If you do that, you will have a new variable called $my_var which has nothing to do with the field in your class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.