Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

2.6.0RC2 - Cacheability of Plugins not PHP5 compatible

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Fri Nov 07, 2003 8:21 pm    Post subject: 2.6.0RC2 - Cacheability of Plugins not PHP5 compatible Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Nov 07, 2003 11:11 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Fri Nov 07, 2003 11:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Nov 07, 2003 11:58 pm    Post subject: Reply with quote

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. Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon Nov 10, 2003 8:01 am    Post subject: Reply with quote

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. Confused
Back to top
View user's profile Send private message
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Mon Nov 10, 2003 11:26 am    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Nov 10, 2003 11:36 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Mon Nov 10, 2003 11:49 am    Post subject: Reply with quote

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
View user's profile Send private message
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Wed Apr 14, 2004 1:27 pm    Post subject: Reply with quote

Hi,

are there any news on how to resolve this problem? Smile
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Apr 14, 2004 1:43 pm    Post subject: Reply with quote

at the moment i favour a solution similar to this one:
http://www.phpinsider.com/smarty-forum/viewtopic.php?p=7730#7730
(use the tokenizer to replace $this by $_smarty, the tokenizer is default in php5).
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Wed Apr 14, 2004 2:32 pm    Post subject: Reply with quote

Hi,

that's good news. Smile
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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Apr 14, 2004 2:36 pm    Post subject: Reply with quote

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. Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Thu Apr 15, 2004 2:37 pm    Post subject: Reply with quote

Okay, thank you.
Do you plan to commit that change into cvs soon?
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Apr 15, 2004 3:06 pm    Post subject: Reply with quote

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! Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Thu Apr 15, 2004 8:40 pm    Post subject: Reply with quote

Seems to work so far. Thanky you.
Good work! Smile
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP