5

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.

Community
  • 1
  • 1
Shafiul
  • 2,832
  • 9
  • 37
  • 55
  • If you are really concerned about performance, you should have already made some measurements for different scenarios. – Jon Sep 08 '11 at 09:08
  • No, actually I've not done any simulation based on different possible scenarios. I lack knowledge in software testing. Any help will be highly appreciable – Shafiul Sep 08 '11 at 09:11
  • Access the global (e.g. call a do-nothing function on it) in a loop. Access it some other way (no global) in another loop. [Measure the time taken by each loop](http://stackoverflow.com/questions/6245971/accurate-way-to-measure-execution-times-of-php-scripts). – Jon Sep 08 '11 at 09:19
  • Using the shared scope (that's what global variables *in PHP* actually are) has no relation to OOP. It's just another language feature. Trying to avoid something *at all costs* does usually include performance. – mario Sep 08 '11 at 10:15

3 Answers3

4

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:

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
  • 1
    P.S if you are interested into MVC and php best practices, then StackOverflow's chat could be a good place to frequent ( the php channel ). – tereško Sep 08 '11 at 10:39
2

I've solved a similar problem in Agile Toolkit by creating a different pattern for adding object and using it system-wide. This passes a property to a newly created objects called "api" which always references Application class.

Application class is not really a God class but it delegates all sorts of functionality to system controllers, pages etc. This screencast explains how the very basic objects are structured, it might be something you are also looking for:

http://www.youtube.com/watch?v=bUNEHqYVOYs

romaninsh
  • 10,606
  • 4
  • 50
  • 70
1

Firstly, whilst you are concerned with performance, you may want to read http://en.wikipedia.org/wiki/God_object first - your "core" class sounds like a "God object", which is a fairly well-established anti pattern.

In terms of performance - the best way to find out is to test it. If you're writing a framework, I'm assuming you're writing unit tests to validate it's behaviour; it's not hard to extend that unit testing to include simple performance metrics. You might also invest in test scripts using JMeter or similar to exercise a "reference implementation" of a few pages built using the framework. You'll get far better information on your specific situation from doing this than by trying to optimize the design based on Stack Overflow's collective knowledge of the general way things work.

In general, I'd say that having a global class shouldn't impact performance too much, as long as it isn't doing much work. Simply loading the class into memory, parsing it, etc. does have a performance impact - but it's not likely to be measurably slower than any other routes you might take.

If, however, your "core" class does lots of initialization logic when it's accessed by the page, it's obviously going to impact performance.

Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52
  • Thanks. The `Core` class actually holds some objects of other important classes. `Core` does not have lots of member functions itself, but it has objects of other classes which in together, makes the framework working. How bad is that? :| – Shafiul Sep 08 '11 at 09:54
  • 1
    "Good" and "bad" are not hugely useful terms - you have to look at the forces affecting your project and evaluate your choices in that context. For instance, if the project you're building is tiny, not going to evolve or grow, and the same team are always going to be working on the project, the "god class" is not a huge deal. Ditto with performance - usually, optimizing for performance means trading off some other characteristic (commonly it's maintainability). Sometimes, that's necessary - but there are also times when "fast enough" is good enough. – Neville Kuyt Sep 08 '11 at 12:43