smarty/4-PHP8-compatible-code-fixes #1147
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
##smarty/4 added PHP8 compatible code fixes
PHP 8.1 → 8.4 Compatibility Fixes for Smarty
Executive Summary
This document details all refactoring changes applied to the Smarty template engine to ensure PHP 8.1 through PHP 8.4 compatibility. All changes are non-breaking, backward-compatible, and focused on removing deprecation warnings without altering functionality.
Test Status: ✅ All fixes validated with PHP 8.3.27
1. Deprecated String Interpolation Syntax
${}→ ConcatenationIssue
PHP 8.2 deprecated the
"${var}"string interpolation syntax in favor of"{$var}"or concatenation.File:
libs/sysplugins/smarty_internal_runtime_make_nocache.phpBefore:
After:
Rationale: The
${var}syntax is deprecated in PHP 8.2. Using{$var}provides the same functionality without triggering warnings.File:
libs/sysplugins/smarty_internal_compile_block.phpBefore:
After:
Rationale: In this code generation context, explicit concatenation is clearer and avoids the deprecated syntax entirely.
2. Deprecated
strftime()Function →smarty_strftime()PolyfillIssue
PHP 8.1 deprecated
strftime()function entirely. It was removed in PHP 8.1.0 due to platform inconsistencies and lack of thread-safety.Solution: Comprehensive Polyfill Function
File:
libs/functions.phpCreated
smarty_strftime()function with the following features:strftime()on PHP < 8.1strftime()format codes todate()equivalents%e(day with leading space)Implementation:
Format Mappings Supported:
%d,%e,%j,%u,%w%V(ISO-8601 week)%b,%B,%h,%m%g,%G,%y,%Y%H,%I,%l,%M,%p,%P,%r,%R,%S,%T%z,%Z%c,%D,%F,%s,%x%a,%A%n(newline),%t(tab),%%(literal %)File:
libs/plugins/modifier.date_format.phpBefore:
After:
Rationale: Direct replacement with polyfill removes need for error suppression and provides clean, maintainable solution.
File:
libs/plugins/function.html_select_date.phpBefore:
After:
Rationale: Consistent use of polyfill across all date/time formatting functions.
3. Dynamic Property Creation Warnings →
#[AllowDynamicProperties]AttributeIssue
PHP 8.2 introduced deprecation warnings when creating properties on objects that weren't explicitly declared, unless the class has the
#[AllowDynamicProperties]attribute.Files Modified
libs/sysplugins/smarty_internal_data.phpBefore:
After:
Impact: This is the base class for Smarty, Smarty_Internal_Template, and Smarty_Data, so the attribute is inherited by all child classes.
libs/sysplugins/smarty_internal_block.phpBefore:
class Smarty_Internal_BlockAfter:
Rationale: Block classes are dynamically generated at runtime via string concatenation in
smarty_internal_compile_block.php, and child classes add properties dynamically.libs/sysplugins/smarty_template_compiled.phpBefore:
After:
Rationale: The
@propertyannotation indicates dynamic property usage. Adding the attribute prevents PHP 8.2+ warnings.libs/sysplugins/smarty_internal_templatecompilerbase.phpBefore:
After:
Rationale: Compiler uses dynamic properties for state management and post-compile callbacks.
Classes Already Having
#[AllowDynamicProperties](Pre-existing)Smarty_VariableSmarty_SecuritySmarty_Internal_TemplateSmarty_Internal_Extension_Handler4. Testing & Validation
Manual Testing Performed
✅ Smarty class instantiation
✅
smarty_strftime()polyfill function✅ Variable assignment
✅ Smarty_Variable creation
✅ Smarty_Data creation
✅ Dynamic property assignment
✅ Template compilation
✅ Date formatting with various strftime formats
Test Environment
Results
5. Summary of Changes
libs/functions.phplibs/plugins/modifier.date_format.phplibs/plugins/function.html_select_date.phplibs/sysplugins/smarty_internal_runtime_make_nocache.phplibs/sysplugins/smarty_internal_compile_block.phplibs/sysplugins/smarty_internal_data.phplibs/sysplugins/smarty_internal_block.phplibs/sysplugins/smarty_template_compiled.phplibs/sysplugins/smarty_internal_templatecompilerbase.phpTotal Files Modified: 9
Total Lines Changed: ~180
Breaking Changes: 0
API Changes: 0
6. Known Limitations & Future Considerations
strftime() Polyfill
IntlDateFormatterfrom theintlextension.%U,%W,%C) have no direct date() equivalents and are mapped to empty strings.Dynamic Properties
#[AllowDynamicProperties]will continue to allow dynamic properties indefinitely. This is by design for backward compatibility, but future major versions could consider stricter typing.7. Recommendations
For Smarty Users
strftime(), update them to usesmarty_strftime().For Smarty Maintainers
smarty_strftime()in date formatting examples.8. References
Appendix: Before/After Comparison Matrix
strftime() Format Conversion Examples
%Y-%m-%d2024-11-11Y-m-d%B %e, %YNovember 11, 2024F j, Y%l:%M %p3:45 PMg:i A%A, %B %dMonday, November 11l, F d%Y-%m-%d %H:%M:%S2024-11-11 15:45:30Y-m-d H:i:sDocument Version: 1.0
Last Updated: November 11, 2025
Prepared By: Senior PHP Architect
Status: ✅ Complete - All fixes applied and validated