0

Ok so I'm sure there's a nice way to do this. But I'm stuck.

Basically, I have a folder of 'custom handler's that run functions on sets of products. Folder (and namespace) structure looks like this

HANDLERS
-MAIN
--Generate.php
--List.php
-THREAD
--Generate.php
--List.php
-TEST
--Generate.php
--List.php

Inside the 'generate.php' and 'list.php' are static functions, which they all share. In my controller, I want to call one of these handlers based on an atrribute of the model. Something like:

\Handlers\$product->handler\Generate::go();

If the product used 'Thread' it would call

\Handlers\Thread\Generate::go();

Is there an easy way to do this?

Cheers!

1 Answer 1

4

You can use call_user_func to call a method on a dynamic class name, e.g.:

<?php
namespace MyPackage;

class Foo
{
    public static function bar()
    {
        echo 'Hello';
    }
}

$className = "\\MyPackage\\Foo";
call_user_func([$className, 'bar']);
Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh, could've guessed this one! I should've just tried it instead of trying to think of some unique way of handling this. Will accept in 5 mins :)

Your Answer

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.