I am searching for the best way to call a class method from another class, without having to use Global to fetch the class instance, cus as i understand now that "Global is evil" !
Here is some code to explain it more:
class Assets{
public $assets = array();
public function add($asset){
$this->assets[] = $asset;
}
}
Now i wanna call the Assets/call method from with in here ..
$assets = new Assets;
class Form{
public function __construct(){
global $assets;
$assets->add('Form');
}
}
Is using Global in such scenario is THAT bad ? If so, what other way is considered to be the best ?
PS: I need to work with only one instance of the class; means that i don't want to create a new instance inside of the second class.
global
is "bad"? Try to focus on that first. Develop your knowledge of the approach before criticizing the language flatly and you will be better received when looking for help. The advantages will be more readily apparent once you're a little more familiar with why injection is important (it's used all over the Java world, for example.) – DeaconDesperado May 28 '13 at 18:38