Bridging Global State and Class Encapsulation
You currently have a global variable defined outside your class scope:
$MyNumber = 42;
Inside myClass, you are accessing this value via $GLOBALS['MyNumber'] in every method. While this works, it couples your internal logic to the global namespace, making testing difficult and code harder to read. You want to refactor this so that a class property holds the value, allowing methods to access it simply as $this->classNumber.
To achieve the syntax var $classNumber = ???, you need to understand how PHP handles variable scope assignment. You cannot directly assign a global variable by name (e.g., global $MyNumber) inside a property declaration; that syntax is reserved for function scopes. Instead, you must retrieve the value first and then assign it.
The Assignment Pattern
The cleanest way to initialize a class property with a global value is to read from the $GLOBALS superglobal array within the constructor or via __set. Since you need to perform arithmetic (adding 100), the constructor is the ideal location.
Here is how you structure the class:
class myClass {
private $classNumber;
public function __construct() {
// Retrieve global value and apply modification
$baseValue = $GLOBALS['MyNumber'];
if (!isset($baseValue)) {
throw new RuntimeException('Global variable MyNumber is not set.');
}
// Assign the modified value to the private property
$this->classNumber = (int)$baseValue + 100;
}
private function foo() {
// Now access cleanly without touching global scope
$privateNumber = $this->classNumber;
// Use $privateNumber...
}
}
Why Not global in Property Declarations?
You might wonder if you can write var $classNumber = global $MyNumber + 100;. This is syntactically invalid. The global keyword inside a variable declaration context is not supported by PHP's parser in that manner. It only works when declaring variables inside a function to create shortcuts to the global scope, such as:
function example() {
global $MyNumber;
}
Properties are defined at the class level, which exists outside of any specific function stack frame where global logic applies directly. Therefore, you must use an assignment statement inside a method (like the constructor) to bridge the gap.
Trade-offs and Pitfalls
Moving from $GLOBALS to a private property improves encapsulation, but it introduces dependency management challenges.
- Runtime Dependency: Your class now relies on the global variable existing at the exact moment the object is instantiated. If you instantiate
myClassbefore defining$MyNumber, or if the variable name differs slightly, your code will crash with aRuntimeException(if you add validation) or a notice about undefined indexes. - Testability: While hiding global state seems good, relying on external globals to seed class properties makes unit testing awkward. You would have to manipulate
$GLOBALS['MyNumber']before running tests, which can lead to test pollution where one test affects another if the cleanup isn't perfect. A better pattern for production code is often passing values via dependency injection or using a static configuration provider rather than pulling from globals. - Performance: Accessing
$this->classNumberis negligible in performance compared to array lookups, but it does add a tiny overhead of method call resolution compared to a global lookup. However, the readability gain far outweighs this micro-optimization.
Conclusion
To transform a global variable into a class property, you must retrieve the value from $GLOBALS and assign it to the property within the class lifecycle, typically inside the __construct method. This approach allows you to modify the data before assignment while keeping your internal methods decoupled from the global namespace.
