samedi 25 juillet 2020

Force return statement on caller function

I am just curious if it's possible to force parent method to return a value from within method called in that parent method? Let's say I have:

    public function myApiEndpoint()
    {
      // I DO NOT want to to have return statement here
      $this->validOrUnprocessable();

      // some other code

      //return value
      return $someValue;
    }
    public function validOrUnprocessable()
    {
       if ($condition) {
         ... here goes the code that forces return statement on myApiEndpoint function without putting the word `return` in front of this call...
       }
    }

So in other words validOrUnprocessable method, when it needs to do so forces or tricks PHP into thinking that myApiEndpoint returns the value. I do not want to use return statement when validOrUnprocessable is called or any if conditions.

I do know other ways of doing what I want to do but I wanted to know if something like that is possible. I am not interested in any workarounds as I know very well how to implement what I need to achieve in many other ways. I just need to know if this what I described is possible to do exactly how I described it.

I did try to get there with reflections and other scope related things but so far no luck. Any ideas?

Just to add. I am doing this because I want to check how far I can push it. I am building a tool for myself and I want it to be as convenient and easy to use as possible.

If it's not possible I have another idea but that's a bit out of the scope of this post.





Aucun commentaire:

Enregistrer un commentaire