php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41450 New operator feature request
Submitted: 2007-05-20 18:07 UTC Modified: 2007-08-21 12:33 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: dead-krolik at gmail dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 6CVS-2007-05-20 (CVS) OS: any
Private report: No CVE-ID: None
 [2007-05-20 18:07 UTC] dead-krolik at gmail dot com
Description:
------------
It's not a bug, it's a new language operator suggestion. When I use other languages and PHP it's often were a one task - return values from function. I suggest a new operator and a don't know any analogs in other languages (may be I now small count of languages).

Reproduce code:
---------------
Task - return values from function with accumulating of results. For example, when we read strings from file and want return array of this strings (may be modified) from function we must accumulate/collect all values in array or in string:

function read_array($in) {

    $out = array();

    foreach(file($in) as $str) {

        $str = trim($str);
  
        //some modifications

        $out[] = $str;
        //OR $all_str .= $str
    }

    return $out;
    //OR return $all_str
}

It's a very often task. And may be it's will be useful for programers have two new operators - for strings and for arrays, that accumulate values within all function body and at the end return ALL strings/elements automatically.

For example:

function read_array($in) {

    foreach(file($in) as $str) {

        $str = trim($str);
  
        //some actions

        return_array $str;//accumulate $str to internal temporary array, that will be returned at the end of function
    }
}

And "return_string" for strings temporary buffer.

-- 
Sorry for my terible English. With best regards Dead Krolik.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-20 22:33 UTC] michael at chunkycow dot com dot au
Howdy

you can use a reference parameter to accumulate values like this.

function read_array($in, &$accumulator) {

    $out = array();

    foreach(file($in) as $str) {

        $str = trim($str);
  
        //some modifications

        $out[] = $str;
        //OR $all_str .= $str
        $accumulator[] = $str;
        // or $accumulator .= $str;
    }

    return $out;
}

Now after you call the function the last parameter will hold all of the new values, this is a nasty way though and I would suggest you read some more about references(http://au2.php.net/manual/en/language.references.php).
You can ofcourse use a global variable to store this aswell if it was an OO based thing (don`t use global keyword if your not using OO dear god please!).
+1 for marking this as bogus, bad programming practice should not a language make :)
 [2007-08-21 03:44 UTC] dead-krolik at gmail dot com
I know about references. Reference parameter it's the same like internal variable. I suggest new operator. And if we will use it - we will not need to define and use any variables.
 [2007-08-21 09:17 UTC] michael at chunkycow dot com dot au
What you want is fundamentally wrong and the wrong way to look at it, having some sort of vahalla of acumulating data would be slow, bloated and incredibly inflexible.
So to get the facts straight, you don`t want to define 1 extra variable because your lazy and then request a language feature to support your aversion to doing the right thing ?

Oh just thought of something, you can always use ob_start and friends it's a global buffer of sorts or you can always just define your own.
 [2007-08-21 12:33 UTC] johannes@php.net
There is no real need for such a syntax.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 17:01:29 2024 UTC