Smarty How to call a const member of a PHP class

Source: Internet
Author: User
Tags parse error
Smarty How to call a const member of a PHP class
PHP Content:
Require ("libs/smarty.class.php");
Class My_class
{
Const PWD = "Gogo";
var $username = "Deep Space";
}
$class _obj = new My_class;
echo $class _obj->username. '
';
echo $class _obj::P wd. '
';
$smarty->assign (' class_obj ', $class _obj);
$smarty->display (' Eg_4_2.tpl ');
?>
TPL content:


{$class _obj->username}

{$class _obj::P WD}




For $class_obj->username
Both PHP and TPL are used normally ...

For $class_obj::P WD
Normal use in PHP
Error when using TPL

Please be familiar with the guidance under, thank you!

------Solution--------------------
You will be required to abide by the Smarty rules or smarty expelled.
------Solution--------------------
Http://wenku.baidu.com/view/c2aa98ef551810a6f52486af.html
------Solution--------------------
Try the My_class::P WD

Not yet, look at this.
Http://www.smarty.net/docs/en/advanced.features.static.classes.tpl
------Solution--------------------
Pass the class name over. $smarty->assign (' My_class ', my_class);
TPL calls:
{PHP}
echo My_class::P wd;
{/php}
PHP 5.3.0 can only be used after the $class _obj::P WD call. The previous version is not available. I do not know your PHP version is how much?
------Solution--------------------
Still tangled, do I try that way? Where not?
------Solution--------------------
Don't know what you're wrestling with:
PHP Code
  ";} $obj = new C (); $a = c::aaa; $smarty->assign ("A", $a); $smarty->assign ("str1", $str 1); $smarty->assign ("str2", $str 2); $smarty->display ("test.html");? >
------Solution--------------------
I see it. But there's nothing I can do.
------Solution--------------------
$smarty->assign ("str1", $str 1);
$smarty->assign ("str2", $str 2);
To test the data, it's useless, erase
------Solution--------------------
Understanding your needs, it is likely that Smarty has not yet received this demand,
So there's no such function, you can put this requirement on their website,
or write yourself a plugin.

discuss

Reference:

Try the My_class::P WD

Not yet, look at this.
Http://www.smarty.net/docs/en/advanced.features.static.classes.tpl


I saw the link you gave me.
The link is about static classes.

And my problem is the static members of the class ...
Thank you for your answer ...

------Solution--------------------
So unreasonable a need to mention it? The development of the time to work
------Solution--------------------
Are you still struggling with the problem?

{$class _obj::P WD}

has been translated into
tpl_vars[' Class_obj ']->value::P wd;? >



Although
$class _obj = new My_class;
echo $class _obj->username. '
';
echo $class _obj::P wd. '
';
Can get the right results (but he is not strict)

But what about this?
$class _obj->x = new My_class;
echo $class _obj->x->username. '
'; It can output deep space here.
echo $class _obj->x::P wd. '
';//The error will be here:
Parse error:syntax error, unexpected T_paamayim_nekudotayim, expecting ', ' or '; ' in

------Solution--------------------
I have checked the manual, only the variable can pass the value, the constant cannot pass the value like this!!
This is the only way:
php: $PWD = $class _obj::P wd; $smarty->assign ("PWD", $PWD);

tpl:{$PWD}
------Solution--------------------
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.