php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62519 Find out about a function's call context
Submitted: 2012-07-10 04:10 UTC Modified: 2021-09-27 13:30 UTC
From: awilliams at linkme dot com dot au Assigned: cmb (profile)
Status: Wont fix Package: Unknown/Other Function
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
14 - 3 = ?
Subscribe to this entry?

 
 [2012-07-10 04:10 UTC] awilliams at linkme dot com dot au
Description:
------------
I would like to be able to tell if a function was called from a context that is expecting a return value or not.  Additionally what type is expected.
 
 eg('hello');                // called not expecting a return value
 echo eg('hello').' world';  // called expecting a string


function eg($s)
{
  if(return){  // suggested overloading of the return keyword to report the function context.
    return ($s);
  }
  else{
    echo $s;
  }
}

Additionally 
 gettype(return);
 and
 is_string(return);
 is_array(return); 
etc


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-10 14:01 UTC] nikic@php.net
PHP is a dynamically typed language. It can't in the general case know what type is expected. Simple example if f1() + f2(). Here int, float and array would be viable return types.

Whether the return value is used or not is known to the engine and also exposed to internal functions.

But I don't think that it makes sense to expose it to userland functions. Functions shouldn't behave different based on whether the return value is used or not.
 [2012-07-11 11:31 UTC] awilliams at linkme dot com dot au
I would like to present a more concrete example of why I think this would be an elegant solution to a coding dilemma I have.

I wrote a couple of functions to generate html for <select> mark-up from mySql tables and php arrays.
Some times I want to call them directly and I want then to output the html, other times I want to use them in string expressions, in which case I want them to return the html.

As it stands I would have to do this using different function names, which looks messy.

To implement this I have two choices, maintain two separate functions doing almost the same thing.  That is never best practice.  Sooner or later you forget to do a change to both of them.

Declare the string returning function and call it from the string printing version.  This is wasteful of stack and call time resources, I try not to code like that.

I am left looking at this thinking it would be a really elegant solution if the function its self could tell what context it was called in and either echo or return the output as required.

I agree that it’s a somewhat unconventional suggestion.  I wouldn't expect it of 'C'.  I write in PHP because many things are simpler an more flexible here, and this would definitely add a new form of flexibility that I have a use for.

Thank you and that's my last word on it.
Alan
 [2012-07-12 01:51 UTC] johannes@php.net
You could pass an additional parameter to the function 

  function ($foo, $return) {
     if ($return) {
         return $foo;
     } else {
         echo $foo;
     }
   }

Many people would say that changing the behavior of a function from it's use case is bad.

I'm also not sure we can implement this without adding overhead to *all* function calls, which we won't like to do.
 [2012-10-26 19:11 UTC] j_holland at hotmail dot com
The reason to expose the calling context is so that the function can confirm it's correct:
    function calculate_something($args){
      if (get_calling_context() == "VOID") {
        print "ERROR: must be called in non-void context!";
        exit;
      }
      return do_expensive_calculation($args);
    }
This might be unnecessary in your own code but it's VERY useful when writing libraries. Perl has this function (wantarray) and I use it whenever I write libraries.

Yes, this function can be misused, but so can most functions.

Overloading the return keyword is a terrible way to implement this. Make it a clearly named function, like get_calling_context.

Finally, it would probably be sufficient to simply return void/non-void context, but more specific context is known:
  print function();    // the RV will be evaluated as a string
  $a = 3 + function(); // the RV will be treated as a number
  if (function()));    // the RV will be treated as a Boolean
etc.
 [2021-09-27 13:30 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-09-27 13:30 UTC] cmb@php.net
> Many people would say that changing the behavior of a function
> from it's use case is bad.

That.  Still, users can write such functions if they which (and
PHP itself has var_export() and maybe others), but we should not
endorse this by adding "automatic" support.  Therefore, I'm
closing as WONTFIX.

If anyone still wants to have this feature, please pursue the RFC
process[1].

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC