Answer the question
In order to leave comments, you need to log in
Which option to get rid of global variables in an OOP PHP application?
1. There is a main engine written in a procedural style, which is being handled by other developers. It is replete with global variables.
2. There is my addition for this engine, which I try to write in the OOP style.
Question: in order not to write global $variable, $variable1, etc. in each method of each class, which method is better:
1) Make a trait and add it to classes that require access to global variables, with a similar method:
public function &__get($name)
{
return $GLOBALS[$name];
}
$variable = $GLOBALS['variable'];
$variable1 = $GLOBALS['variable1'];
// и т. д.
Answer the question
In order to leave comments, you need to log in
Decide what we are writing - OOP or shit code with globals.
In the second case, it doesn't matter what shades of smell this shit will have.
In the first case, throw out all this hell and do it humanly, passing variables to the constructor / methods as parameters.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question