PHP程式碼生成器介紹

jieforest發表於2012-09-07
PHP generators are awesome

I’m not sure if you heard but PHP 5.4 is going to support generators. This is fantastic news if you ask me.

Although PHP 5.3 has been out a while, few hosting providers have fully adopted it so I have little hope getting this awesomeness on the burner just yet – so I put together a little script. very similar to the yield functionality that will be implemented by our good friends at PHP.

For those python/Vala/Ruby/Java people out there this may not look like a big deal but it is… It shows that PHP, with all of its faults, is becoming a better language every day.

This script. by no means optimized and should be seen only as a way to possibly implement Yielding in PHP and how to use in Magento for a finer and better data interaction with exports and imports. Notice that I unset the data as much as possible even tough the garbage collector should take care of it.

As you can see there are 2 different versions, one for < 5.3 and one for 5.3 and greater. Since I can’t do math in my head I added an extra convert function and you should know that little function adds more memory to the mix… so take it out if you think your data is skewed. Try reading a file with this approach or a big array.
[code]<!--?php 

echo "Current Memory: " . convert(memory_get_usage(true)), "

function convert($size) {
    $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
    return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}

function yield() {
    //echo 'Memory before processing the data: ', memory_get_usage(),'  in ',FUNCTION, ':
';
    $args = func_get_args();

    //do something with data
    //echo '
';
    //var_dump($args);
    // echo '
';
    // echo 'Memory after processing the data: ', memory_get_usage(),'  in ',FUNCTION, ':
';

    unset($args);
    return;
}

function process(array $someData = array()) {
    // do some computations that generates $data

    foreach ($someData as $data) {

        // echo 'Memory before yielding: ', memory_get_usage(),'  in ',FUNCTION, ':
';
        call_user_func('yield', $data);
        // echo 'Memory after yielding: ', memory_get_usage(),'  in ',FUNCTION, ':
';
    }
}

function process_53(array $someData = array()) {
    // do some computations that generates $data

    foreach ($someData as $data) {

        // echo 'Memory before yielding: ', memory_get_usage(),'  in ',FUNCTION, ':
';
        call_user_func(function() {
                    //echo 'Memory before processing the data: ', memory_get_usage(),'  in ',FUNCTION, ':
';
                    $args = func_get_args();

                    //do something with data
                    //echo '
';
                    //var_dump($args);
                    // echo '
';
                    // echo 'Memory after processing the data: ', memory_get_usage(),'  in ',FUNCTION, ':
';

                    unset($args);
                    return;
                }, $data);
        // echo 'Memory after yielding: ', memory_get_usage(),'  in ',FUNCTION, ':
';
    }
}

echo '
Let\'s do something fun
';
echo '
Starting memory: ', convert(memory_get_usage(true)), '
';
$longString = str_repeat(md5('php yield'), 2000); //heavy operation
$someData = array_fill(5, 60000, $longString); // this alone will raise your memory usage to the moon
echo '
Memory after getting the data: ', convert(memory_get_usage(true)), '
';

process_53($someData);
unset($someData);
echo '
End memory: ', convert(memory_get_usage(true)), '
';
[/code]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-742850/,如需轉載,請註明出處,否則將追究法律責任。

相關文章