-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHP 7.2 Deprecations: create_function #25
Comments
The only alternative to My advice would be:
In either case, if you know a way to convince stupid bullheaded PHP devs to finally implement damn arrow functions which has been in RFC for ages and has been requested since forever, please do so. If these retarded fucks finally implement arrow functions, I can finally drop support for string lambdas and forget this abomination ever existed. If I understand the deprecation process correctly, leaving Note that some string lambdas like |
The benchmark shows that is slightly faster to create a function with |
The |
@Bilge You have a point. I'll add a minor 2.x version I think, as master is currently unfinished 3.0. Hm, PHP 7.3 is available. Thank gods, they didn't remove deprecated functions. That would happen only in 8.0, right? I hope that by that time, these idiots will finally add arrow functions instead of implementing random garbage like instanceof on literals and numeric keys in objects. Ugh. Hm, looks like I need to remove support for |
Yes, they would never remove any deprecated functions in a minor release. But that's besides the point, the deprecation warnings themselves are the problem in this case. |
If you need to upgrade more than one It's tested on 30 various (and really weird :)) cases. -$callback = create_function('$a', 'return "<cas:proxy>$a</cas:proxy>";');
+$callback = function ($a) {
+ return "<cas:proxy>{$a}</cas:proxy>";
+}; Includes concat (.), string quotes and inclined function calls: -$func = create_function('$atts, $content = null','return "<div class=\"' . $class_list . '\">" . do_shortcode($content) . "</div>";' );
+$func = function ($atts, $content = null) use ($class_list) {
+ return "<div class=\"{$class_list}\">" . do_shortcode($content) . "</div>";
+}; Do you want to automate the hard work? 1. Instal Rectorcomposer require rector/rector --dev 2. Create config# rector.yml
services:
Rector\Php\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector: ~ 3. Upgrade your Codevendor/bin/rector process src --config rector.yml --dry-run
vendor/bin/rector process src --config rector.yml Enjoy! |
@Athari It's been a month since I raised this. Any update on the minor tag? |
…g. Back-ported Travis config from master.
@Bilge Back-ported changes from master which affect supported PHP versions, added tag v2.4.2. I may have broken something in the process (messed up rebase, had to reset branches), so please test. |
- PHP versions below 7.0 are not tested under, should not be shown as supported. - php-coveralls now has is a different package See Athari#25
Looking at a0e782a I can say there's one thing missing surely. If you don't test on PHP 5.6, do not declare support for it. #43 should fix that problem, if there is a problem (I'm not entirely sure what went where, GitHub isn't helping). (CI's failing on this commit. I think I've fixed these non-numeric values before, should be easy to cherry-pick. Though master build is all green, so guess it's all right.) |
@TomasVotruba IIRC |
How is that relevant in case of removed function? |
Sure, when it's removed, it's removed. When it's not, |
Piling up deprecations didn't proove as good practice. Better solve 5 deprecations per minor version, than 20 at once. |
Any news on this? I am still getting a lot of warnings on v2.4.2. |
Shall we hard-fork this? |
@andre719mv @sanmai What is your problem exactly? The code works. The error is muted. If you don't use string lambdas, the deprecated function in never executed. Where do you get "a lot of warnings" from? Especially in plural form. From analysis tools? PHP 7.4 still hasn't been released. I don't see any benefits of releasing "YaLinqo 3.0" with the only feature being "removed feature" to satisfy a deprecation in a minor release of PHP 7.2, just to release YaLinqo 4.0 a month later with proper argument type hints which would make sense in PHP 7.4 supporting arrow functions. If you prefer to inconvenience yourself with forks instead of configuring your analysis tools, feel free to do so. In the meantime, I can offer adding a branch where |
Version numbers are free.
…On Mon, 2 Sep 2019, 14:12 andre719mv, ***@***.***> wrote:
Hi!
Yes, I have some analysis tool on my site to monitor all errors and
warnings. Warnings from this lib has filled a lot of space and it`s hard to
see other problems.
[image: image]
<https://user-images.githubusercontent.com/22563994/64102938-a5ecb080-cd79-11e9-84ae-5213a904e24d.png>
Thanks for explanation. Now I see that there is no good way to fix it
before PHP 7.4. No problem. I`ll add exception to analysis tool.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#25?email_source=notifications&email_token=AADS4YXHH5QSNUCLKG5HQD3QHUGMRA5CNFSM4EQ2HST2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5VZF5A#issuecomment-527143668>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AADS4YVUHIYHGZ2E5WO4IHLQHUGMRANCNFSM4EQ2HSTQ>
.
|
And warnings are not. Here's a benchmark I cooked from a thin air: $start = microtime(true);
for ($i = 0; $i < 1000; $i++) {
$fn = @create_function('$a,$b', 'return log($a * $b);');
}
var_dump(microtime(true) - $start);
$start = microtime(true);
for ($i = 0; $i < 1000; $i++) {
$fn = function ($a, $b) {return log($a * $b);};
}
var_dump(microtime(true) - $start); Code with create_function is about 100 times slower because of the muted warning.
See for yourself: https://3v4l.org/13afF If |
@sanmai Let's run a test which reflects how the library actually works: <?php
$start = microtime(true);
$fn = @create_function('$a,$b', 'return log($a * $b);');
$v = 0;
for ($i = 0; $i < 1000; $i++)
$v += $fn($i, $i);
echo("create_function: " . (microtime(true) - $start) . "\n");
$start = microtime(true);
$v = 0;
for ($i = 0; $i < 1000; $i++)
$v += eval("return log($i * $i);");
echo("eval: " . (microtime(true) - $start) . "\n");
$start = microtime(true);
$fn = fn($a, $b) => log($a * $b);
$v = 0;
for ($i = 0; $i < 1000; $i++)
$v += $fn($i, $i);
echo("arrow function: " . (microtime(true) - $start) . "\n");
$start = microtime(true);
$fn = function ($a, $b) { return log($a * $b); };
$v = 0;
for ($i = 0; $i < 1000; $i++)
$v += $fn($i, $i);
echo("anonymous function: " . (microtime(true) - $start) . "\n"); Output:
As you can see, the "eval" version is horrendously slow. "Fixing" the deprecation warning by following the suggestion from the developers of PHP and replacing The "create_function" version is still slower than anonymous and arrow functions (1.5x), of course, but it's a known compromise between performance and conciseness. YaLinqo as a whole is a compromise between performance and readability as you do lose performance compared to direct built-in function calls. It's typical for developers of PHP to break things before fixing and then break them again. In this case, they decided to deprecate a function before providing a valid alternative. PHP 7.4 has breaking changes too, which aren't suited for a minor version release, and it breaks more than a dozen of top packages. That's just how PHP is developed. |
Kind reminder =). |
Working on it. |
With PHP 8.0 release, this is broken! The code no longer works. |
+1 for fixing this to gain PHP8 compatibility |
This library is abandoned. |
Although the library seems to be dead, I successfully managed to convert the few calls in my code that triggered the usage of This was before: $r->OptionValues =
E::from($model->Options)->select('$v->Value')->toArray(); And this is how I migrated it to: $r->OptionValues =
E::from($model->Options)->select(function ($v) {return $v->Value;})->toArray(); I.e. I replaced the string with my code, that was converted by the YaLinqo to a function by simply providing the anonymous function by myself. As of now, this seems to work just perfectly. Hopefully it stays this way. |
Thanks for the workaround. Successfully implemented it and got our code to work! |
I think that that is the only solution going forward. By the way, you can shorten the lamdba function by using |
@jorrit Is this "short syntax" compatible with older PHP versions? According to https://www.php.net/manual/en/functions.arrow.php this only works with PHP 7.4 and newer. Unfortunately I have to support older versions, too. |
You're right about that. My suggestion is just for codebases that only need to support 7.4 and up. |
Can confirm, using an anonymous function within Yalinqo |
… with eval. Fixed PHPUnit dependency (PHP_Timer).
Latest news
Future plansThe master branch does contain some useful changes besides failed optimization (most of it isn't even in the repo), including a few extra methods and API fixes (with breaking changes). At the moment I'm considering releasing everything as 3.0. Maybe with extra Considering nobody seems to be interested in new functionality, including me, the difference between 2.0 and 3.0 will be 5% cleaner API, 3 extra methods, dropped string lambdas and PHP 8+ requirement. Let me know what you think. Assuming anyone is still following this issue. |
I think steady maintenance is exactly what this library needs. Just keeping up with the changes in PHP and merging the occasional contribution seems like a good plan. Thanks for your efforts! |
Apparently Packagist requires a hook now. It failed to install one automatically, so I added it manually. Seems to work now, the 2.5 version of the package is up. |
As as follow-up to #24: the create_function has been deprecated in php 7.2.
Would it be possible to update Utils::createLambdaFromString to avoid using that function ?
Thanks
The text was updated successfully, but these errors were encountered: