2

The point is i have getDates() method and i want to get this method's name as string but not to run this method. Actually it looks like next:

$object->getResultExecutionMethod(convertmMethodNameToString($object->findDates()));

getResultExecutionMethod($methodName) {
  switch($methodName) {
    case convertmMethodNameToString($this->findDates()):
    return $getDatesStatus;
    break;

    case convertmMethodNameToString($this->anotherMethodOfThisClass()):
    return $anotherMethodOfThisClassStatus;
    break;
  }
}

In 1 class i have a lot of methods, and a lot of variables that comply this methods execution status. Calling convertmMethodNameToString() and putting there my method i want to get execution status by this method. So how i can implement convertmMethodNameToString() function?

Link
  • 669
  • 7
  • 20
  • Can you do it the other way around? http://php.net/manual/en/functions.variable-functions.php – Mario Chueca Jun 13 '16 at 11:54
  • You can just replace `convertmMethodNameToString($this->findDates()` with `"findDates"` in the code above, and replace `convertmMethodNameToString($this->anotherMethodOfThisClass())` with `"anotherMethodOfThisClass"`. There appears to be no need for the `convertmMethodNameToString` function at all – Chris Lear Jun 13 '16 at 11:54
  • @mario-chueca , Do you mean to use this way? `$methodName = "findDates()"; $object->methodName();` If yes - i'm still dependent of hardcode each method name; – Link Jun 13 '16 at 12:01
  • I'm not sure I understand the question, but you can replace `convertmMethodNameToString($object->findDates())` with `"findDates"` as well. I can't see why you need the method at all. Maybe there's some code missing here. – Chris Lear Jun 13 '16 at 12:03
  • http://stackoverflow.com/questions/1424846/can-you-get-a-method-name-from-within-a-method-in-php read this. – Hiren Makwana Jun 13 '16 at 12:31

1 Answers1

1

You could maybe benefit from the magical __call method. You could say that if you need a status for a certain method, you would call a method with the same name, but with the suffix "Status". The nice thing is that you don't actually have to create all those methods with "Status" at the end, but can use a trap for it.

Additionally, you can use __FUNCTION__ to get the name of the function that is running. This is not interesting for getting the status, but might be for setting it.

Here is some example code:

class myClass {
    // Use an array to keep the statusses for each of the methods you have:
    private $statusses = [
        "findDates" => "my original status",
        "anotherMethodOfThisClass" => "another original status"
    ];
    public function findDates($arg) {
        echo "Executing " . __FUNCTION__ . ".\n";
        // Set execution status information:
        $this->statusses[__FUNCTION__] = "last executed with argument = $arg";
    }
    // ... other methods come here

    // Finally: magic method to trap all undefined method calls (like a proxy):
    public function __call($method, $arguments) {
        // Remove the Status word at the end of the method name
        $baseMethod = preg_replace("/Status$/", "", $method);
        // ... and see if now we have an existing method.
        if(method_exists($this, $baseMethod)) {
            echo "Returning execution status for $baseMethod.\n";
            // Yes, so return the execution status we have in our array:
            return $this->statusses[$baseMethod];
        }
    }
}

// Create object
$object = new myClass();
// Execute method
$object->findDates("abc");
// Get execution status for that method. This method does not really exist, but it works
$status = $object->findDatesStatus();
echo "Status: $status\n";

The above code outputs this:

Executing findDates.
Returning execution status for findDates.
Status: last executed with argument = abc

See it run on eval.in

trincot
  • 317,000
  • 35
  • 244
  • 286