[Smarty]”Call of unknown method ‘assign_by_ref”エラーが出る理由

PHPで、Smartyを使ったフレームワークを使用していると、以下のエラーが出る場合が有ります。


(!) Fatal error: Uncaught exception 'SmartyException' with message 
'Call of unknown method 'assign_by_ref'.' 
 
in ...\php\pear\Smarty\sysplugins\smarty_internal_templatebase.php on line 806



これは、Smartyを使用しているクライアント側のコードがassign_by_ref()をコールしているが、そのメソッドをSmartyが用意していないというエラーです。SmartではVer2以前では、assign_by_ref()メソッドが有りましたが、ver3からはassignByRef()に名前が変わったのが理由です。

対処するにはクライアント側のコードを変更するか、使用するSmartyのバージョンを2.xに戻します。


Smarty3のメソッド名命名規則の変更

Smartyはバージョン3で関数の命名規則が、アンダーバーでつなぐ形式からキャメルケースに変更されたようです。この辺の詳しい話はREADMEに載ってるので、引用しておきます。

The Smarty 3 API has been refactored to a syntax geared
for consistency and modularity. The Smarty 2 API syntax is still supported, but
will throw a deprecation notice. You can disable the notices, but it is highly
recommended to adjust your syntax to Smarty 3, as the Smarty 2 syntax must run
through an extra rerouting wrapper.
 
Basically, all Smarty methods now follow the "fooBarBaz" camel case syntax. Also,
all Smarty properties now have getters and setters. So for example, the property
$smarty->cache_dir can be set with $smarty->setCacheDir('foo/') and can be
retrieved with $smarty->getCacheDir().
 
Some of the Smarty 3 APIs have been revoked such as the "is*" methods that were
just duplicate functions of the now available "get*" methods.




ざっくり日本語に訳すと以下の様な感じです。

一貫性とモジュール化の観点から、Smarty3 APIのリファクタリングをしました
Smarty2のAPI関数はサポートされていますが、非推奨のNoticeが出ます。
Noticeを無効にすることもできますが、Smarty3への変更を行っておく事を強く推奨します。
 
基本的に、Smarty3のメソッドはfooBarBaz()みたいな感じで、キャメルケースになっています。
あと、プロパティはgetter/setterが用意されています。
例えば、プロパティ"$smarty->cache_dir"は、以下のメソッドでset/getできます。
   $smarty->setCacheDir('foo/')
   $smarty->getCacheDir()
 
また、Smarty2まで存在していたisXxxx()的なメソッドは、一部getXxxx()的な感じに変わってたりもします。




具体的には、以下のメソッド名になります。

Here is a rundown of the Smarty 3 API:
 
$smarty->fetch($template, $cache_id = null, $compile_id = null, $parent = null)
$smarty->display($template, $cache_id = null, $compile_id = null, $parent = null)
$smarty->isCached($template, $cache_id = null, $compile_id = null)
$smarty->createData($parent = null)
$smarty->createTemplate($template, $cache_id = null, $compile_id = null, $parent = null)
$smarty->enableSecurity()
$smarty->disableSecurity()
$smarty->setTemplateDir($template_dir)
$smarty->addTemplateDir($template_dir)
$smarty->templateExists($resource_name)
$smarty->loadPlugin($plugin_name, $check = true)
$smarty->loadFilter($type, $name)
$smarty->setExceptionHandler($handler)
$smarty->addPluginsDir($plugins_dir)
$smarty->getGlobal($varname = null)
$smarty->getRegisteredObject($name)
$smarty->getDebugTemplate()
$smarty->setDebugTemplate($tpl_name)
$smarty->assign($tpl_var, $value = null, $nocache = false)
$smarty->assignGlobal($varname, $value = null, $nocache = false)
$smarty->assignByRef($tpl_var, &$value, $nocache = false)
$smarty->append($tpl_var, $value = null, $merge = false, $nocache = false)
$smarty->appendByRef($tpl_var, &$value, $merge = false)
$smarty->clearAssign($tpl_var)
$smarty->clearAllAssign()
$smarty->configLoad($config_file, $sections = null)
$smarty->getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true)
$smarty->getConfigVariable($variable)
$smarty->getStreamVariable($variable)
$smarty->getConfigVars($varname = null)
$smarty->clearConfig($varname = null)
$smarty->getTemplateVars($varname = null, $_ptr = null, $search_parents = true)
$smarty->clearAllCache($exp_time = null, $type = null)
$smarty->clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
 
$smarty->registerPlugin($type, $tag, $callback, $cacheable = true, $cache_attr = array())
 
$smarty->registerObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
 
$smarty->registerFilter($type, $function_name)
$smarty->registerResource($resource_type, $function_names)
$smarty->registerDefaultPluginHandler($function_name)
$smarty->registerDefaultTemplateHandler($function_name)
 
$smarty->unregisterPlugin($type, $tag)
$smarty->unregisterObject($object_name)
$smarty->unregisterFilter($type, $function_name)
$smarty->unregisterResource($resource_type)
 
$smarty->compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
$smarty->clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
$smarty->testInstall()
 
// then all the getters/setters, available for all properties. Here are a few:
 
$caching = $smarty->getCaching();      // get $smarty->caching
$smarty->setCaching(true);             // set $smarty->caching
$smarty->setDeprecationNotices(false); // set $smarty->deprecation_notices
$smarty->setCacheId($id);              // set $smarty->cache_id
$debugging = $smarty->getDebugging();  // get $smarty->debugging

関連記事

コメントを残す

メールアドレスが公開されることはありません。