smarty 怎么调用 php类的常量成员

WBOY
Release: 2016-06-13 13:14:28
Original
800 people have browsed it

smarty 如何调用 php类的常量成员
php 内容:
require("libs/Smarty.class.php");
class my_class 
{
const PWD = "gogo";
var $username = "深空";
}
$class_obj = new my_class;
echo $class_obj->username.'
';
echo $class_obj::PWD.'
';
$smarty->assign('class_obj', $class_obj);
$smarty->display('eg_4_2.tpl');
?>
tpl内容:


 {$class_obj->username}

 {$class_obj::PWD}




对于$class_obj->username
在 php 和 tpl 两边使用都正常。。。

对于$class_obj::PWD
在php使用就正常
在tpl使用就报错

请熟悉的指点下,谢谢!

------解决方案--------------------
您需要遵守 smarty 的规则,否则将被 smarty 开除
------解决方案--------------------
http://wenku.baidu.com/view/c2aa98ef551810a6f52486af.html
------解决方案--------------------
试下 my_class::PWD

还不行看这里
http://www.smarty.net/docs/en/advanced.features.static.classes.tpl
------解决方案--------------------
把类名传过去。$smarty->assign('my_class', my_class);
tpl调用:
{php}
echo my_class::PWD;
{/php}
PHP 5.3.0之后才可以用 $class_obj::PWD 调用。之前的版本是不能的。不知道你php版本是多少?
------解决方案--------------------
还在纠结啊,按我那种方法试了吗? 哪里不行?
------解决方案--------------------
不知道你在纠结什么:
PHP code

";
}
$obj = new C();
$a = C::AAA;

 $smarty->assign("A",$a);
 $smarty->assign("str1",$str1);
 $smarty->assign("str2",$str2);
 $smarty->display("test.html");

?>

------解决方案--------------------
我是看明白了。但我无能为力。
------解决方案--------------------
$smarty->assign("str1",$str1);
 $smarty->assign("str2",$str2);
为测试数据,没用,删掉
------解决方案--------------------
理解你的需求, 很有可能smarty还没有收到这方面的需求,
所以也没有做出这个功能,你完全可以上他们的网站提下这个需求,
或者自己写个plugin

Copy after login
探讨

引用:

试下 my_class::PWD

还不行看这里
http://www.smarty.net/docs/en/advanced.features.static.classes.tpl


看了你给的链接
链接里讲的是 静态类

而我的问题是 类的静态成员。。。
还是要谢谢你们的回答。。。

------解决方案--------------------
这么无理的需求也提?开发的时候要变通
------解决方案--------------------
还在纠结这个问题吗?

{$class_obj::PWD}

被翻译成了
tpl_vars['class_obj']->value::PWD;?>



虽然
$class_obj = new my_class;
echo $class_obj->username.'
';
echo $class_obj::PWD.'
';
可以得到正确的结果(但他是不严密的)

但这样呢?
$class_obj->x = new my_class;
echo $class_obj->x->username.'
'; //这里能输出 深空
echo $class_obj->x::PWD.'
';//这里就要报错了:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ';' in 

------解决方案--------------------
我查过手册,只有变量可以那样传值,常量不能这样传值!!
只能这样:
php:$PWD=$class_obj::PWD; $smarty->assign("PWD",$PWD);

tpl:{$PWD}
------解决方案--------------------
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!