Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php global variable overhead in a framework

I'm currently developing a framework which uses an object of a Core class (this class has huge functionality & makes the framework working). The framework follows MVC architecture & has loosely coupled Model, Control, View classes. Theses classes need a reference to the Core class heavily. What I've done so far is: creating single object of the Core class & referencing to it by PHP keyword global in Model, Control, View classes.

I don't like using this approach mainly because:

  • This way is not true object oriented way in my sense
  • The IDE (netbeans) can't provide documentation to the object of the Core class - a pain for developers who will be using this framework.
  • I'm really worried about performance issues - have no idea whether global is slower or whatever.

I've searched & did not find any information regarding performance issue. I've also searched stackoverflow & found Does using global create any overhead? & The advantage / disadvantage between global variables and function parameters in PHP? etc links but they don't contain much information. Right now my main concern is performance, so please help.

like image 207
Shafiul Avatar asked Sep 08 '11 09:09

Shafiul


1 Answers

I must agree with NevilleK, that you Core` class sounds line an God Object antipattern.

And for anyone dumb enough to suggest use of singletons/registries i would suggest to do a little research on the subject. They create the same global state as your classical global variables.

Global state is not so much matter of performance ( though in php it has some minor impact ), but it created untestable and tightly coupled code.

You really should look into the Dependency Injection. That might show you another way , which does not require to have such a Core class in your code.


Some additional videos for you:

  • Global State and Singletons
  • Don't Look For Things!
  • Advanced OO Patterns
  • Cake is a Lie
  • Clean Code: Arguments
like image 79
tereško Avatar answered Sep 30 '22 11:09

tereško