Replace create_function() with anonymous function

Discussions and requests related to new CMSimple features, plugins, templates etc. and how to develop.
Please don't ask for support at this forums!
Post Reply
cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Replace create_function() with anonymous function

Post by cmb » Thu Feb 26, 2015 12:45 am

Hello Community,

since we recently decided to lift our minimum requirements for XH 1.7 to PHP 5.3, I like to suggest to replace all create_functions() in the core and the standard plugins with anonymous functions for better comprehensibility and to avoid the quoting hell.

While preparing a respective patch, I've noticed a bug in meta_tags. Fixing this bug already gets rid of a create_function(), so we're left with only two create_function() calls.

I suggest the following patch:

Code: Select all

Index: cmsimple/classes/Mailform.php
===================================================================
--- cmsimple/classes/Mailform.php	(revision 1490)
+++ cmsimple/classes/Mailform.php	(working copy)
@@ -404,8 +404,9 @@
                     $text = '';
                 }
             } while ($text != '');
-            $body = 'return \'=?UTF-8?B?\' . base64_encode($l) . \'?=\';';
-            $func = create_function('$l', $body);
+            $func = function ($line) {
+                return '=?UTF-8?B?' . base64_encode($line) . '?=';
+            };
             return implode($this->_linebreak . ' ', array_map($func, $lines));
         }
     }
Index: cmsimple/functions.php
===================================================================
--- cmsimple/functions.php	(revision 1490)
+++ cmsimple/functions.php	(working copy)
@@ -2849,7 +2849,12 @@
  */
 function XH_highlightSearchWords($words, $text)
 {
-    usort($words, create_function('$a, $b', 'return strlen($b) - strlen($a);'));
+    usort(
+        $words,
+        function ($a, $b) {
+            return strlen($b) - strlen($a);
+        }
+    );
     $patterns = array();
     foreach ($words as $word) {
         $patterns[] = '/' . preg_quote($word, '/') . '(?![^<]*>)/isuU';
Index: plugins/meta_tags/Metatags_view.php
===================================================================
--- plugins/meta_tags/Metatags_view.php	(revision 1490)
+++ plugins/meta_tags/Metatags_view.php	(working copy)
@@ -40,9 +40,6 @@
 {
     global $sn, $su, $plugin_tx, $pth, $onload, $bjs;
 
-    $func = create_function('&$data', '$data=str_replace("\"", """, $data);');
-    array_walk($page, $func);
-
     $lang = $plugin_tx['meta_tags'];
 
     $my_fields = array('title', 'description', 'keywords', 'robots');
If this patch will not be accepted, we should not forget to fix the meta_tags bug in XH 1.7, if that had not already happened in the meantime.
Christoph M. Becker – Plugins for CMSimple_XH

manu
Posts: 1090
Joined: Wed Jun 04, 2008 12:05 pm
Location: St. Gallen - Schweiz
Contact:

Re: Replace create_function() with anonymous function

Post by manu » Fri Feb 27, 2015 8:20 am

Aah, new possibilities..
I just learned by changing create_functions() into anonymous functions, especially in preg_replace_callback (probably on array_walk() too), there is no scope on outside variables/global variables (except the ported "matches" argument). Use the USE Keyword to catch outside/parent variables.

Code: Select all

$c = preg_replace_callback($regi,
        function($matches) use ($visible_at){
        ... 
http://php.net/manual/en/functions.anonymous.php see example #3. The enlighting explanation is a bit hidden.

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Replace create_function() with anonymous function

Post by cmb » Fri Feb 27, 2015 12:22 pm

manu wrote:I just learned by changing create_functions() into anonymous functions, especially in preg_replace_callback (probably on array_walk() too), there is no scope on outside variables/global variables (except the ported "matches" argument). Use the USE Keyword to catch outside/parent variables.
Yes, good point. Also note the slight difference between use and global with regard to global variables.

Also it appears to be noteworthy that the $this reference is not availble inside of closures in PHP 5.3 -- an unfortunate pitfall.
Christoph M. Becker – Plugins for CMSimple_XH

Post Reply