1

I understand the reasons for not using statics in Java.

However, I'm currently developing OO code in PHP. I use DAOs with the goal of keeping my queries in one place so I can easily find them. I also instantiate some DAOs so I can incorporate pagination in some (relevant) queries. In many cases, it's not necessary and so I tend to just create static methods (even though technically I don't think I can call that a DAO) in the form:

$info = schemeDAO::someFunction($variable);

I may need only that single method during a page refresh (i.e. a specific value in a header file).

I may need to instantiate the same DAO a hundred times as objects are created and destroyed.

 $dao = new myDao();
 $info = $dao->someFunction($variable);

Either way, it seems to me, in PHP at least, wouldn't it be more performance efficient to simply load a static and keep it in memory?

halfer
  • 19,824
  • 17
  • 99
  • 186
user3168961
  • 375
  • 1
  • 10
  • 1
    What is the reason that statics are bad in Java? Why do the same reasons not apply to PHP in your opinion? Why do you need to "instantiate the same DAO a hundred times"? Why do you think statics are so much more efficient? What do you think is being kept in memory here? – deceze Sep 25 '14 at 12:06
  • AFAIK objects are in fact faster than static classes. There shouldn't be a reason for destroying and creating though... – Teson Sep 25 '14 at 12:06
  • Here's the source: http://stackoverflow.com/questions/1472721/performance-of-static-methods-vs-functions – Teson Sep 25 '14 at 12:12
  • OO methodology, testing, application memory, etc. "What do you think is being kept in memory here?" My assumption, a blueprint, the class structure. To access a static function class instantiation is not required. To access a method, an object of the type must be created in full. Since GC will remove any DAO object created when it ceases to be referenced, if another class uses the same dao is instantiated again elsewhere, it seems to me additional overhead would result since the object must be created yet again? Hence this question... user247245's link gives me the information I wanted. thanks – user3168961 Sep 25 '14 at 13:02
  • 1
    Sure, if you're constantly creating objects and letting them fall out of scope, obviously there's overhead in recreating them. However, why wouldn't you instantiate the object once and then pass them to all methods that use them? See [How Not To Kill Your Testability Using Statics](http://kunststube.net/static/). – deceze Sep 25 '14 at 14:20

1 Answers1

0

While the static access is acceptable (to an extent), with the dynamic approach you can pass the object transitively to the 3rd side object via dependency, (otherwise also the transitive call the transition of dependency would have to be initiated from the original class), which needs not to be pushed some data, but rather the dependency decides and pulls the data/method it needs be it multiple times in a single method. Otherwise way it can only return, while instance can be called, not-separated wrapper method logic from data. Instance inline code seems to be shorter, and when you remove an instance, all their calls complain at that moment, whereas static class continues to preserve unnoticed in the code as they don't need the instantiation prerequisite. Static classes preserve their state in between various objects, and methods contexts and thus are not automatically "reset" as it is with the 'new construct'. Instances encourage more transparent pure functions approach - passing parameters. When you pass an object, you don't separate the service logic from it's data structure, when you pass only the array data structure, the execution logic is lost in transit or separated into another place and must be eventually called intransparently statically when not-passed - pure functions concept.

I would use comparison with Einsteins vs Newton's equations. In some cases, they look totally the same. But to be pretty content I would use more versatile instances or service locator singletons, vs static classes. On the other side, the less "versatile" static classes might be initially easier to implement, especially if you don't plan on rocket with them as far as to the space orbit as you might get with instances. Similarly as with private attributes you signal they are not passed anywhere, pure functions, though it might less often signalize also the bad, being called from anywhere.

FantomX1
  • 1,577
  • 2
  • 15
  • 23
  • also discussed more detailedly in here https://softwareengineering.stackexchange.com/questions/111938/which-is-a-better-practice-helper-methods-as-instance-or-static – FantomX1 Sep 04 '20 at 23:19