php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78822 Returning from multiple function calls
Submitted: 2019-11-16 01:34 UTC Modified: 2019-11-17 11:54 UTC
From: mail-roboczy-dla-forow at wp dot pl Assigned:
Status: Suspended Package: *General Issues
PHP Version: Next Minor Version OS: All
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:
24 + 34 = ?
Subscribe to this entry?

 
 [2019-11-16 01:34 UTC] mail-roboczy-dla-forow at wp dot pl
Description:
------------
function analyze($anyParameter)
{
   foreach($anyParameter as &$v)
      b($v);
}

function b(&$obj)
{
   foreach($obj as $line)
   {
      $res=c($line); 
   }
   if(...) 
   {
      ...
      $res=b(substr($line,3)); /* recurrent function */
      ...
   } 
   ...
}

function c(&$line)
{
   if($line=='1') 
      return 3;
   if($line=='2') 
      return 4;
   if($line=='unexpected data')
      [analyze] return false; /* return from previous calls 
                                 and return 'false' from 
                                 'analyze' function and 
                                 continue execution from
                                 next instruction after 
                                 last 'analyze' call. */
   return -1;
}

Test script:
---------------
No test script, the functionality will help faster return from multiple calls, for example analyzing jpeg, gif or odt files. It is the common situation that the analyzing function identyify unexpected data and the analysis must by broken due to nonsense data and returns error code. 

It is possible to write code without this enhancement but the code will be longer  and will contain additional conditions slowing execution of multiple nested calls. 

Expected result:
----------------
additional syntax 

[functionName] return result;


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-11-16 01:35 UTC] requinix@php.net
-Status: Open +Status: Feedback -Package: PHP Language Specification +Package: *General Issues
 [2019-11-16 01:35 UTC] requinix@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.


 [2019-11-16 11:55 UTC] rowan dot collins at gmail dot com
It sounds like what you're looking for is exceptions. The main difference is that exceptions don't have to know where they will be caught, so they're a lot more flexible. In your example function c could throw an exception, and function analyze would catch it and continue to the next input item.
 [2019-11-16 17:36 UTC] mail-roboczy-dla-forow at wp dot pl
But... "try catch" will not slow execution of code? 
"Try catch throw" needs definition of subclass of Exception to catch only it.
 [2019-11-17 11:54 UTC] cmb@php.net
Re performance: if in doubt, measure!  Also I suggest that you
reconsider the excessive usage of references; see
<http://schlueters.de/blog/archives/180-References-Still-bad-in-PHP-7.html>.

Re this feature request: this is certainly controversial, so would
require discussion on the internals mailing list[1].  Feel free to
forward the request to there.  For the time being, I'm suspending
this ticket.

[1] <https://www.php.net/mailing-lists.php>
 [2019-11-17 11:54 UTC] cmb@php.net
-Status: Feedback +Status: Suspended
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 08:01:28 2024 UTC