0

I have a switch statement

 switch ( $id ) {
 case 'abc': return 'Animal';
 case 'xyz': return 'Human';
 //many more
 }

I am returning class names,and use them to call some of their static functions using call_user_func(). Instead I can also create a object of that class, return that and then call the static function from that object as $object::method($param)

 switch ( $id ) {
 case 'abc': return new Animal;
 case 'xyz': return new Human;
 //many more
 }

Which way is efficient? To make this question broader : I have classes that have mostly all static methods right now, putting them into classes is kind of a grouping idea here (for example the DB table structure of Animal is given by class Animal and so for Human class). I need to access many functions from these classes so the switch needs to give me access to the class

asked Jun 14, 2012 at 17:02
6
  • I'm not proficient in PHP but could you return a delegate/function pointer/callback? Commented Jun 14, 2012 at 17:11
  • @MattDavey I need the class not a single function Commented Jun 14, 2012 at 17:15
  • 1
    Are you sure you need the class? It seems from the question you just want to execute one or more functions... Commented Jun 14, 2012 at 17:17
  • I need more than one static functions from that class, so I return the class Commented Jun 14, 2012 at 17:49
  • Please move your clarifying comments (the above one and the one on Dibbeke's answer) into the question itself, they are easy to miss as comments. Commented Jun 14, 2012 at 19:25

3 Answers 3

4

Obviously, needlessly calling a constructor for the sole reason of calling a static method is less efficient. Apparently there is some kind of meta-class encapsulating the static behavior of Animal/Human or perhaps the static method has the wrong location. The situation reeks of a modelling/design error, but there isn't enough context to determine which.

answered Jun 14, 2012 at 17:15
7
  • IANAPHPprogrammer, but it could also be a limitation of the language in use. At an abstract level, it does seem to be the wrong way about it. Commented Jun 14, 2012 at 17:30
  • PHP certainly has some odd limitations and edge cases that can be cumbersome... Commented Jun 14, 2012 at 17:30
  • I am starting to implement these classes and right now they have only a bunch of static methods, so there isn't any constructor atm Commented Jun 14, 2012 at 17:56
  • If you only have static methods, you are most likely abstracting at the wrong level. A static method is essentially a 'normal' method of the meta-class (the class of the class). But since you probably don't have a language in which you can express behavior of meta-classes, you should model the meta-class as a normal class instead and have it produce instances, just like you'd do with a factory pattern (you'd return factories with behavior that allows you to instantiate objects implementing an interface). Hope this makes sense to you. Commented Jun 14, 2012 at 18:58
  • By the way, if it doesn't, please say so. I can't become a good teacher without feedback :) Commented Jun 14, 2012 at 19:01
1

Definitely go with the static-calling route. Constructing an object for the sole purpose of calling a static method on it is wasteful, but since you've decided to use PHP, you shouldn't be worrying about optimization on this level anyway. What you should worry about is side effects - there is no rule in PHP that says a constructor can't have any side effects, and if it does, it can bite you badly. For example, you might construct an object, and the constructor happens to output stuff to the client, and then you try to redirect, but header() fails because you've already sent output.

As an even better solution, see if you can come up with a proper design. I can see two sane routes you could take here:

a) Wrap each of the static methods you want to call in a free function, and pass that (as of PHP 5.3, you can use anonymous functions for this, which, BTW, also provide a neat way of controlling variable scope beyond the normal two-level rules).

b) Wrap each of the static methods in a non-static method of a class that implements a common interface defining this method; return instances by interface, and call the interface method. This is essentially your basic factory pattern with behavior classes.

answered Jun 14, 2012 at 17:30
2
  • addendum to b) make the static methods non-static in the first place, then you don't need wrappers and the code will actually be object oriented. Commented Jun 16, 2012 at 8:54
  • @MichaelBorgwardt: Good point. I assumed that the static methods were basically set in stone; if they aren't, then yes, making them non-static is the way to go. Commented Jun 16, 2012 at 9:59
1

can you do factory methods in PHP?

Interfrace IMethodsINeedToCall()
 Method1INeed();
 Method2INeed();
and your Animal / Human classes implement the interface:
Class Animal:IMethodsINeedToCall
 ...implement methods
Class Human:IMethodsINeedToCall
 ...implement methods 
IMethodsINeedToCall animalResults = CallSwitchStatementAndRetrunObject('animal');
animalResults.Method1INeed();
animalResults.Method2INeed();
IMethodsINeedToCall humanResults = CallSwitchStatementAndRetrunObject('human');
humanResults.Method1INeed();
humanResults.Method2INeed();

not sure if PHP supports that methodology... I"m sure it does to some extent, but seems like a clean approach.

answered Jun 14, 2012 at 17:31
1
  • +1 This is basically what I'm trying to get at in my comments in the question Commented Jun 14, 2012 at 18:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.