Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Should plugin-functions initiate auto-loading?

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
cartbeforehorse
Smarty n00b


Joined: 17 Feb 2016
Posts: 4

PostPosted: Wed Feb 17, 2016 4:41 pm    Post subject: Should plugin-functions initiate auto-loading? Reply with quote

Smarty version 3.1.29.

My application has an auto-loader function which which I put on the PHP auto-load stack with spl_autoload_register(). This works fine on the whole (and worked fine in older versions of Smarty).

When instigating the plug-in function "load_po" in my *.tpl file, my Auto-loader function raises an error in the application, saying that it cannot find a class called "Smarty_Internal_Compile_load_po".

At first, I wondered why the Smarty auto-loader wasn't being instigated. However, with further investigation, I realised that it was being instigated, but failing to find the class (hence passing the call onto my secondary auto-loader).

I traced the source of the auto-loading initiator to a file called "smarty_internal_templatecompilerbase.php". Function "callTabCompiler()". At line 860/861, the logic appends the string the string "Smarty_Internal_Compile_[b]" to the function name ("[b]load_po"), and then checks whether it exists as a class, as follows:

Code:

$class_name = 'Smarty_Internal_Compile_' . implode('_',$_tag);
if (class_exists($class_name) &&...


Of course, this class does not exist in either the Smarty framework, or my application, so both auto-loaders fail to find it. The problem (as I said earlier) is that my auto-loader errors when a file is not found. (This can be remedied, of course).

However, putting the work-around aside, it seems to me that it is a a bug to search for the class "Smarty_Internal_Compile_load_po". When I change the function "callTagCompiler()" from

Code:
class_exists($class_name)

to
Code:
class_exists($class_name,false)

the problem goes away.

Does the community agree that this is a bug?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Wed Feb 17, 2016 6:40 pm    Post subject: Reply with quote

This is a bug.
In your autoloader.
Autoloader MUST NOT throw errors in case it is unable to load something.
You never know, if your loader is last to be called, and you throwing errors prevent any other loaders to be called.
Back to top
View user's profile Send private message
cartbeforehorse
Smarty n00b


Joined: 17 Feb 2016
Posts: 4

PostPosted: Wed Feb 17, 2016 10:58 pm    Post subject: Reply with quote

AnrDaemon wrote:
This is a bug.
In your autoloader.
Autoloader MUST NOT throw errors in case it is unable to load something.
You never know, if your loader is last to be called, and you throwing errors prevent any other loaders to be called.


Fair enough, I suppose.

But at the same time, I feel it worth noting that my autoloader is designed for an end-user application. The framework is not designed to be extended or added to in the same was that Smarty and other middle-tier solutions are. If a file cannot be found, then there is a problem somewhere, that needs to be brought to attention in some way.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Wed Feb 17, 2016 11:43 pm    Post subject: Reply with quote

Once more, if all autoloading means are exhausted, but a class still can not be instantiated, PHP will throw an error by itself.
You don't need to do anything at all to make it happen. The work has been cut out for you years ago.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Tue Mar 01, 2016 1:15 am    Post subject: Reply with quote

If your autoloader throws an error if a class is not found it breaks the functionality of the PHP class_exits() function.

If you change the Smarty code to class_exists(..., false) you will brake the function of Smarty.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins 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