4
function a(){
    b(1); // Returns true
    b(0); // Echoes "Just some output"
}

function b($i_feel_like_it){
    if($i_feel_like_it){
        return return true;
    }else{
        echo "Just some output";
    }
}

Is it possible to call a "return" function from within a different function?

The purpose for this is i have a class with lots of functions.. and instead of writing a bunch of code that determines whether they should return some value, i want to simply put a function like "validate()" and have the function call a return if necessary, otherwise continue with the function.

Just wondering if it's possible to do this.

5
  • If you want that called functions affect on the flow control of the function which calls them, that's an use case for exceptions. I added an example using exception for validation, see if that helps you. Commented Jun 22, 2013 at 1:26
  • i have about 40 functions that return some value.. i have another function that can "overwrite" those values. when those functions are called, i want a simple "check_overwritten_value()" to either return the overwritten value, or simply continue if none is found. i know how to make it work with 2 lines of code, i just don't want to repeat the same code for 40 functions that's all Commented Jun 22, 2013 at 1:28
  • Yeah, well, but you know, this is a design problem really, not a language one. It's alright that you didn't knew beforehand what exactly you wanted to do, and then you make some functions and called them. But now that you do know what you want, and it's a bit different than how you made it, I think that's the time for you to change it, IMO -- and not look for another shortcut. Commented Jun 22, 2013 at 1:39
  • i realize it's a design flaw, i did not foresee doing this when i was writing the class, but i have found a solution nevertheless. i know these problems can be eliminated with good design, but i just wanted to know if such a thing existed in php.. i dont know why it's so terrible to just ask a simple question Commented Jun 23, 2013 at 1:51
  • Sorry, I didn't mean to sound too critic. It's very much OK to ask these questions -- I was just trying to provide some guidance, I've been in situations like these before, and looking for the shortcuts ended up being worse. Chill out! :) Also, you can answer your question here with the solution you found. Commented Jun 23, 2013 at 13:25

5 Answers 5

3

In short, NO. Thanks gosh, allowing that would make it a very weird language, where you probably would not rely on the return of any function.

You can throw exceptions, though, check out the manual. That way you can have the called methods affect the flow control in the callee -- try not overuse them to do this, though, because the code can get quite ugly with too much of this.

Here is an example on how to use exceptions for validation:

class ValidationException extends Exception { }

function checkNotEmpty($input) {
    if (empty($input)){
        throw new ValidationException('Input is empty');
    }
    return $input;
}

function checkNumeric($input) {
    if (!is_numeric($input)) {
        throw new ValidationException('Input is not numeric');
    }
    return $input;
}

function doStuff() {
    try {
        checkNotEmpty($someInput);
        checkNumeric($otherInput);
        // do stuff with $someInput and $otherInput
    } catch (ValidationException $e) {
        // deal with validation error here
        echo "Validation error: " . $e->getMessage() . "\n";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

No it is not. You would have to check what b() returns, and return from a() if it is true.

function a() {
    if (b(1) === true)
        return true; // Makes a() return true
    if (b(0) === true)
        return true; // Makes a() echo "Just some output"
}

function b($i_feel_like_it) {
    if ($i_feel_like_it){
        return true;
    } else {
        echo "Just some output";
    }
}

2 Comments

i know how to make it work.. i just don't want to write repetitive code for like 40 functions.. even though it's just a few lines, i was wondering if it's possible
I completely agree. Not possible though.
1

What you are trying isn't possible. Check the manual of return

6 Comments

if b(1) could return "return true" and make function a() call "return true" that's how. i was just wondering if it's possible, but apparently it's not.
i know how return works, i was just wondering if there was anything at all that could accomplish it
I see the example was not very nice. It's because what you are trying isn't possible
If you would know, you would not ask this. ;) Give me a second and I'll add link to documentation.... Often reading the documentation, even about basic things again, will enlight you
i was using "return return true" just as an example of what i was trying to accomplish. obviously that doesn't work, and i already knew 99% that it's impossible to do it, i was just thinking maybe there's some PHP trick that i wasn't aware of that could accomplish this. but thanks anyway
|
0

Template gap.

function a()
{
 b();
 return $a;
}
function b()
{
 c();
 return $b;
}

Trouble is in your mind...

1 Comment

This does not make any sense, what is supposed to happen here except for some undefined variable warnings? Assuming you have defined c()...
0

If you want a() to return true from b(1)'s true then you can use return a();

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.