B
B
Brainbao2022-01-17 23:42:53
PHP
Brainbao, 2022-01-17 23:42:53

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];
  }

2) Or in each method to get to the global variables with:
$variable = $GLOBALS['variable'];
$variable1 = $GLOBALS['variable1'];
// и т. д.

3) Or implement a simple DJ container and add $GLOBALS there as a dependency in order to access the container cell in the right places?
4) What are your options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2022-01-18
@Brainbao

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 question

Ask a Question

731 491 924 answers to any question