0

I have a doubt that maybe someone can answer here. My Code works great, but I am quiet sure, I do it somehow wrong. There has to be an easier and cleaner way to achieve it.

Assume this Class:

class myClass {
    public static function myFunctionOne ($arg_one, $arg_two, $arg_tree) {
        echo $arg_one;
        myClass::$arg_two($arg_tree);
    }
    public static function myFunctionTwo ($arg_four) {
        echo $arg_four;
    }
}

Then you call above Class somewhere (my case a HTML Menu built in a PHP File) as the below:

myClass::myFunctionOne('echo a string', 'myFunctionTwo', 'echo forth value');

This works great, and it outputs:

echo a string echo forth value

But is it legit?

It looks sketchy to me. Can I use Functions like that, to call a Function with Argumetns in a Function with arguments?

6
  • 1
    Yes it is legit. Like this you can dynamically call functions dependent on user input. You might aswell check out php.net/manual/de/function.call-user-func.php . The other question is: Do you really need dynamic function calls? Mostly not. Commented Jul 11, 2016 at 7:28
  • well, I am almost sure, I could do it differently but the above case is just a summarised snippet of what I am doing. I am currently trying to learn some OOP (and yes, above is all but OOP I assume :P). The above is just a small part of a bigger Bootstrap Accordion, where I want to create Tab id's and href's but ALSO call body and menu title with the same function all in one, by just changing the args. Probably there are better ways, but one learns on the way :) Thank you so much for confirming that it is a legit way, and not a "it works casually" case :) Commented Jul 11, 2016 at 7:35
  • 1
    bad approach. Class with only static methods can have hidden dependencies. Commented Jul 11, 2016 at 7:37
  • @RomanPerekhrest that is why I asked. Could you elaborate on this? I am sorry, I am a beginner in OOP Commented Jul 11, 2016 at 7:37
  • 1
    Also, your code doesn't verify if myClass::$arg_two method is defined on myClass Commented Jul 11, 2016 at 7:42

1 Answer 1

1

Can you do this? Absolutely. Should you do this? Maybe not.

In PHP this is called Variable Functions. Basically, any variable with ( attached to it will be used as the name of a function or method to call.

If you need to pass multiple arguments, you can pass the final function's arguments as an array, and then unpack the array when you call the final variable function.

However, it may not be a great design pattern, for other reasons, and this may make the code difficult to maintain. It likely violates the principal of Abstraction for callers of myClass::myFunctionOne() to have to know what methods/functions myClass has and what arguments they take.

Note that I'm using the term "function" here, but if it's inside a class, it's referred to as a "method".

Sign up to request clarification or add additional context in comments.

3 Comments

Note that you can also use self:: myFunctionOne() instead of myClass:: myFunctionOne().
but self:: would only work INSIDE the class, right? I call the class in a separate file.
Correct. self:: is only for inside the class, like in your example. So leave it the way you have it for an external static call like you're talking about.

Your Answer

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