View previous topic :: View next topic |
Author |
Message |
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Fri Nov 07, 2003 8:21 pm Post subject: 2.6.0RC2 - Cacheability of Plugins not PHP5 compatible |
|
|
Hi,
I am using PHP5 Beta2 with Apache 1.3.28 for development.
But Smarty's feature "Controlling Cacheability of Plugins' Output" does not work with PHP5 Beta 2.
I registered a Block-Function "nocache" with the following code:
[php:1:ff51cc2bf4]$this->register_block('nocache', 'tpl_block_nocache', false);[/php:1:ff51cc2bf4]
My "header" template looks like this.
Code: |
<!-- other html code here -->
{nocache}
{if $userdata.userid == 0}
<a href="login.php">Login</a><br />
{else}
Hello {$userdata.username}<br />
<!-- other html code here -->
{/if}
|
The header-Template is included inside the template "index" but I think that is not relevant.
If I turn caching on, Smarty gives me the following error message:
Quote: | Fatal error: Using $this when not in object context in d:\dev\templates\compiled\0^%%185^%%1853008065^header.inc on line 10 |
The relevant content of compiled/....header.inc is the following:
[php:1:ff51cc2bf4]<?php
function _smarty_tplfunc_50928d30752f2a976ae881974118e37a_0(&$this)
{
$_params = $this->_tag_stack[] = array('nocache', array()); tpl_block_nocache($_params[1], null, $this, $_block_repeat=true); unset($_params);while ($_block_repeat) { ob_start(); if ($this->_tpl_vars['userdata']['userid'] == 0): ?>
<a href="login.php">Login</a><br />
<?php else: ?>
Hello <?php echo $this->_tpl_vars['userdata']['username']; ?>
<br />
<?php endif; $this->_block_content = ob_get_contents(); ob_end_clean(); echo tpl_block_nocache($this->_tag_stack[count($this->_tag_stack)-1][1], $this->_block_content, $this, $_block_repeat=false); } array_pop($this->_tag_stack);
}
?>[/php:1:ff51cc2bf4]
The solution is to replace all occurences of $this inside the function _smarty_tplfunc_50928d30752f2a976ae881974118e37a_0 with e. g. $smarty_obj |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Nov 07, 2003 11:11 pm Post subject: |
|
|
i know that this doesn't work with php5. the problem is that the "$this" in the functions is the $this that is used in the compiled templates. and that cannot be changed easily. renaming it to $smarty or so would break BC to a lot of {php}-tags that use $this, a lot for compiler-plugins, maybe some postfilters etc. i'm still looking for a viable solutions to that problem. |
|
Back to top |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Fri Nov 07, 2003 11:27 pm Post subject: |
|
|
Quote: | the problem is that the "$this" in the functions is the $this that is used in the compiled templates. and that cannot be changed easily. |
Are you sure? It seems $this cannnot be used anymore as variablename outside of an object.
But you can still use $this inside the compiled templates and pass it to a function by reference. The only thing is that you cannot use "$this" inside the function.
if you give the smarty object to a function, you can do the following
[php:1:270fda715f]class Smarty {
function xyz() {
static_function($this);
}
}
// won't work with php5
function static_function(&$this) {
$this->var = 'value';
}[/php:1:270fda715f]
this instead should work with php5:
[php:1:270fda715f]class Smarty {
function xyz() {
static_function($this);
}
}
// should work with php5
function static_function(&$smarty_obj) {
$smarty_obj->var = 'value';
}[/php:1:270fda715f] |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Nov 07, 2003 11:58 pm Post subject: |
|
|
i know what you say above. the point is, that quite some third-party code relies on the fact that $this inside the template is the smarty-object displaying it.
another point is, that in php5 accessing properties of $this seems a little faster than accessing any other object's properties (can somebody confirm my observation). so i wouldn't give up $this as the displaying object if possible.
at the moment i see two solutions.
1. making the functions static class functions of their own classes. believe it or not: if you call a static-method from within any object then you have $this inside your static method being a reference to the calling object. in our case $this would become the displaying smarty-objects as desired. but this is an undocumented feature of php (at least i never found any evidence it is actively supported). so this solution would be a hack and may break unexpectedly.
2. replacing $this to $smarty only inside the compiled-include. the compiled templates would stay as they are. this is a little kludgy and prone to errors since the replacer would have to respect the php-syntax to decide which $this is to replace and which is outside of <?php or in single-quotes or escaped or, or, or .... also this solution can never detect the usage of $this as a result of a variable variable and may also clash with other use of $smarty inside the code.
these a the two ideas i have at the moment, but they both are not satisfactory to me right now.
of course i'm open to suggestions. |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Mon Nov 10, 2003 8:01 am Post subject: |
|
|
Ok, when backward compatibility breaks than there's only one solution that comes to my mind:
Whould it be possible to use $smarty_obj only if PHP5 is installed and therefore break the BC. But if PHP4 is running then use the old functionality ($this) instead.
I see no problem of breaking BC if the user thinks he needs to move to PHP5 because PHP5 breaks BC anyway. |
|
Back to top |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Mon Nov 10, 2003 11:26 am Post subject: |
|
|
Personally, I think Smarty's PHP5 Compatibility is much more important than backward compatibility with some third party plugins. As soon as PHP5 comes to final, many webspace providers will certainly update their servers to PHP5.
Do you have an idea, how much plugins would be affected? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Nov 10, 2003 11:36 am Post subject: |
|
|
Troublegum wrote: | As soon as PHP5 comes to final, many webspace providers will certainly update their servers to PHP5. |
of course. for the same reasons they all switched to apache2, right? hello? |
|
Back to top |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Mon Nov 10, 2003 11:49 am Post subject: |
|
|
Well, you are right. Apache2 is not that popular.
But for example MySQL 4. Went to production some months ago and now it is used by 1&1 Puretec. But that's not the point.
My opinion is that php5 compatibility is very important. Smarty must be compatible with php5 in order to survive the transition to php5. Sooner or later, php5 will replace php4.
I do not want to argue about when it happens. I think that's not that important. But If there are no other solutions but to break with backward compatibility to some plugins (not to php4) I think it's worth it. That's what I wanted to say. |
|
Back to top |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Wed Apr 14, 2004 1:27 pm Post subject: |
|
|
Hi,
are there any news on how to resolve this problem? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
|
Back to top |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Wed Apr 14, 2004 2:32 pm Post subject: |
|
|
Hi,
that's good news.
The replacement of $this with $smarty does happen only once after compiling, does it? So, the performance overhead is not that big? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Apr 14, 2004 2:36 pm Post subject: |
|
|
Troublegum wrote: | The replacement of $this with $smarty does happen only once after compiling, does it? So, the performance overhead is not that big? |
right. and it's only done in smarty_core_write_compiled_include(). it's only done for the non-cached parts and it's not triggered if you don't use them or you have caching disabled. |
|
Back to top |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Thu Apr 15, 2004 2:37 pm Post subject: |
|
|
Okay, thank you.
Do you plan to commit that change into cvs soon? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Apr 15, 2004 3:06 pm Post subject: |
|
|
Troublegum wrote: | Okay, thank you.
Do you plan to commit that change into cvs soon? |
i had this lying around for quite some time anyway. i committed it now. please test! |
|
Back to top |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Thu Apr 15, 2004 8:40 pm Post subject: |
|
|
Seems to work so far. Thanky you.
Good work! |
|
Back to top |
|
|