It is difficult for me to find the answer to this question, I am sure that it is in front of me, but I can not collect it.
//Global Variables global $GLOBAL; $GLOBAL['myPluginPath'] = dirname( __FILE__ ); //Main Object class My_Class { public function __construct() { // Initialize Settings require_once $GLOBAL['myPluginPath'] . '/settings.php'; $My_Settings = new My_Settings(); } }
I want to use a variable to make my code feel more organized, I feel that I read my code better when paths are defined this way, and this makes it easy to change the variables so that they apply throughout the code if necessary.
I can make the variables work by writing this inside my methods.
public function WhatEverFunc() { global $GLOBAL require_once $GLOBAL['myPluginPath'] . '/settings.php'; }
The main question here, I wonder if this is bad practice, if there is no better way, then define global $GLOBAL inside each method. If, however, this is bad practice, can you show me good practice?
There is one more thing that I am very interested in. Inside the main __construct you see that I am not using global $GLOBAL because it works without it, but inside that require_once is another class that has methods that should use global $GLOBAL inside them. Hope someone can explain this.
Some say this is bad practice, I think. I read that there is a singleton pattern (bad practice) and a global pattern. Not sure what I did above, I just lost a little what I have to do here with what I'm trying to achieve.
source share