Name

registerClass() — 注册类

说明

void registerClass(string class_name,
string class_impl);

安全设置许可下,Smarty允许你使用registerClass()来注册静态类到模板中使用。


Example 14.36. 注册类

  1. <?php
  2.  
  3. class Bar {
  4. $property = "hello world";
  5. }
  6.  
  7. $smarty = new Smarty();
  8. $smarty->registerClass("Foo", "Bar");
  9. ?>
  10.  
  1. {* 安全设置许可下,模板中可以使用该类 *}
  2. {Bar::$property}
  3. {* Foo 将转换成真实的类 Bar *}
  4. {Foo::$property}
  5.  


Example 14.37. 注册带命名空间的类

  1. <?php
  2. namespace my\php\application {
  3. class Bar {
  4. $property = "hello world";
  5. }
  6. }
  7.  
  8. $smarty = new Smarty();
  9. $smarty->registerClass("Foo", "\my\php\application\Bar");
  10. ?>
  11.  
  1. {* Foo将转换成真实的类 \my\php\application\Bar *}
  2. {Foo::$property}
  3.  

参见 registerObject(), 和 安全.

原文: https://www.smarty.net/docs/zh_CN/api.register.class.tpl