|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
mkahn Smarty Rookie
Joined: 05 Feb 2005 Posts: 10
|
Posted: Tue May 31, 2005 1:38 pm Post subject: Array casts and PHP5 SPL |
|
|
Many of the operations in Smarty (for instance {foreach}) will coerce a parameter argument into an array using array cast. For instance, line 1176 in Smarty_Compiler.class.php from Smarty 2.6.6 (not sure if this is addressed in newer versions):
$output .= "if (count(\$_from = (array)$from)):\n";
The simple array cast does not appear to work as expected with PHP 5.0.4 and objects implementing the SPL ArrayAccess interface. Instead you get one-element array containing the ArrayAccess object.
This causes wierd behaviour, like turning what should be a simple {html_options} list fed from an ArrayAccess object into an <optgroup> type list, with the class name as the optgroup label!
A simple fix would be to replace the straight array-cast operations with a custom _to_array() function, that checked for ArrayAccess-implementing objects and returned them uncast. |
|
Back to top |
|
mkahn Smarty Rookie
Joined: 05 Feb 2005 Posts: 10
|
Posted: Tue May 31, 2005 2:19 pm Post subject: |
|
|
Hopefully the PHP5 internals will silently support (array) and is_array() on SPL objects (my version does not). Until then, smarty would also benefit from a custom _is_array() method that returns true if an object implements ArrayAccess and Iterator.
Note that I didn't mention checking for the Iterator interface above, but it is also essential since many of the (array) casts and is_array() calls in the smarty internals are for purposes of iteration. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue May 31, 2005 6:12 pm Post subject: |
|
|
Hi.
This is more of a feature request than a bug and besides, bug reports are expected to be against the latest release.
As it happens, this behaviour was changed in 2.6.8
http://smarty.php.net/misc/NEWS
Cheers |
|
Back to top |
|
gege2061 Smarty n00b
Joined: 08 Oct 2009 Posts: 1
|
Posted: Thu Oct 08, 2009 2:30 pm Post subject: |
|
|
Hello,
If you are interested, I make a patch for this feature:
Code: | Index: Smarty.class.php
===================================================================
--- Smarty.class.php (revision 3250)
+++ Smarty.class.php (working copy)
@@ -580,7 +580,7 @@
*/
function assign($tpl_var, $value = null)
{
- if (is_array($tpl_var)){
+ if ($this->_is_array($tpl_var)){
foreach ($tpl_var as $key => $val) {
if ($key != '') {
$this->_tpl_vars[$key] = $val;
@@ -612,14 +612,14 @@
*/
function append($tpl_var, $value=null, $merge=false)
{
- if (is_array($tpl_var)) {
+ if ($this->_is_array($tpl_var)) {
// $tpl_var is an array, ignore $value
foreach ($tpl_var as $_key => $_val) {
if ($_key != '') {
if(!@is_array($this->_tpl_vars[$_key])) {
settype($this->_tpl_vars[$_key],'array');
}
- if($merge && is_array($_val)) {
+ if($merge && $this->_is_array($_val)) {
foreach($_val as $_mkey => $_mval) {
$this->_tpl_vars[$_key][$_mkey] = $_mval;
}
@@ -633,7 +633,7 @@
if(!@is_array($this->_tpl_vars[$tpl_var])) {
settype($this->_tpl_vars[$tpl_var],'array');
}
- if($merge && is_array($value)) {
+ if($merge && $this->_is_array($value)) {
foreach($value as $_mkey => $_mval) {
$this->_tpl_vars[$tpl_var][$_mkey] = $_mval;
}
@@ -656,7 +656,7 @@
if(!@is_array($this->_tpl_vars[$tpl_var])) {
settype($this->_tpl_vars[$tpl_var],'array');
}
- if ($merge && is_array($value)) {
+ if ($merge && $this->_is_array($value)) {
foreach($value as $_key => $_val) {
$this->_tpl_vars[$tpl_var][$_key] = &$value[$_key];
}
@@ -674,7 +674,7 @@
*/
function clear_assign($tpl_var)
{
- if (is_array($tpl_var))
+ if ($this->_is_array($tpl_var))
foreach ($tpl_var as $curr_var)
unset($this->_tpl_vars[$curr_var]);
else
@@ -1951,7 +1951,20 @@
return $function;
}
}
-
+
+ /**
+ * Finds whether a variable is an array or implements the ArrayAccess
+ * interface
+ *
+ * @param mixed $var The variable being evaluated
+ *
+ * @return bool
+ */
+ function _is_array($var)
+ {
+ return (bool)($var instanceof ArrayAccess or is_array($var));
+ }
+
/**#@-*/
} |
|
|
Back to top |
|
|
|
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
|