0

I have a class that has 20+ different API function call. Each one is basically similar but there is one funciton I'd like shared between all of the.

For example:

public function first_api_call () {
$response = $client->get($uri, [
    'on_stats'  => function (TransferStats $stats) use ($logger) {
        // do something inside the callable.
        $logger->debug('Request' . $stats->getRequest() . 
                       'Response' . $stat->getResponse() .
                       'Tx Time' . $stat->getTransferTime()
        );
    },
]);
}

Now, I'd like for this tehnically to be on_stats => run_my_stats_function and have the code in that one place so it will take affect whereever it is called.

How do I go about doing this in a class?

1
  • If on_stats should be callable it can be something like array(__CLASS__, 'method')? Commented Oct 16, 2015 at 16:17

1 Answer 1

1

PHP has traits which is a fancy name for "compiler assisted copy/paste", which is exactly what you're looking for :)

https://secure.php.net/manual/en/language.oop5.traits.php

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

Comments

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.