PHP 7.2 Function create_function() is deprecated

前端 未结 4 1255
庸人自扰
庸人自扰 2020-11-22 07:18

I have used create_function in my application below.

$callbacks[$delimiter] = create_function(\'$matches\', \"return \'$delimiter\' . strtolower(\\$matches[1         


        
4条回答
  •  佛祖请我去吃肉
    2020-11-22 07:47

    Automated Upgrade

    If anyone needs to upgrade dozens of create_function() cases in their code to anonymous functions, I work on a tool called Rector.

    It goes through the code and replaces the create_function with anonymous functions 1:1. It's tested on 30 various cases.

    Install

    composer require rector/rector --dev
    

    Setup

    Let's say you want to upgrade code in the /src directory.

    # rector.php
    parameters();
        $parameters->set(Option::PATHS, [
            __DIR__ . '/src',
        ]);
    
        $services = $containerConfigurator->services();
        $services->set(CreateFunctionToAnonymousFunctionRector::class);
    };
    

    Run on your code

    # this is set run, it only report what it would change
    vendor/bin/rector process --config rector.php --dry-run
    
    # this actually changes the code
    vendor/bin/rector process --config rector.php
    
    # the "rector.php" config is loaded by default, so we can drop it
    vendor/bin/rector process
    

    EDIT: Updated 2020-10-31 with PHP Rector 0.8.x syntax

提交回复
热议问题