Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

php72 compat: Countable

Post new topic   Reply to topic    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
Smarty Rookie

Joined: 08 Aug 2003
Posts: 13

PostPosted: Fri Jan 19, 2018 1:28 am    Post subject: php72 compat: Countable Reply with quote

Firstly a huge thank you. Both for smarty in general and for bothering to maintain even smarty 2 to be php72 compat. This saved me huge amounts of time when making some legacy smarty 2 code compat with php72.

There is a tiny link issue here:

In that your announcement for smarty 2.6.31 actually links to 2.6.30.

Then I found another tiny issue due to php72's "count() must be countable" deprecation.

Smarty 2.6.31 seems to compile the following tpl code:
{foreach from=$categories item="category"}

<?php $_from = $this->_tpl_vars['categories']; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }if (count($_from)):
    foreach ($_from as $this->_tpl_vars['category']):

if $categories is not countable php72 complains with:


PHP Warning:  count(): Parameter must be an array or an object that implements Countable

Below are the lines in the compiler class which appear to be responsible. While coping with php72 compat changes I wrote this workaround function:


function qcount($v)
  if (is_array($v) || $v instanceof Countable)
    return count($v);
  return 0;

It's not great, and just a workaround, but it worked for me. I suspect any changes to the compiler class would want to be more clever.

Anyway, given that function, I found that the following patch to the compiler class, gets rid of the php72 warnings, and makes the code work as before:


diff -u libs/Smarty_Compiler.class.php.orig libs/Smarty_Compiler.class.php
--- libs/Smarty_Compiler.class.php.orig 2018-01-19 01:08:58.909041000 +0000
+++ libs/Smarty_Compiler.class.php      2018-01-19 01:09:15.721388000 +0000
@@ -1195,12 +1195,12 @@
         $output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
         if (isset($name)) {
             $foreach_props = "\$this->_foreach[$name]";
-            $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
+            $output .= "{$foreach_props} = array('total' => qcount(\$_from), 'iteration' => 0);\n";
             $output .= "if ({$foreach_props}['total'] > 0):\n";
             $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
             $output .= "        {$foreach_props}['iteration']++;\n";
         } else {
-            $output .= "if (count(\$_from)):\n";
+            $output .= "if (qcount(\$_from)):\n";
             $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
         $output .= '?>';

Hope that's helpful.

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP