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

spl_autoload issue

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


Joined: 31 Oct 2009
Posts: 3

PostPosted: Sat Oct 31, 2009 7:32 pm    Post subject: spl_autoload issue Reply with quote

Hey there,

My system, which uses its own spl_autoload as well, had problems with the spl_autoload_register of smarty.

Code:
Index: core/smarty/Smarty.class.php
===================================================================
--- core/smarty/Smarty.class.php        (revision 3294)
+++ core/smarty/Smarty.class.php        (working copy)
@@ -91,7 +91,7 @@
     spl_autoload_extensions('.php,.inc');
     $registeredAutoLoadFunctions = spl_autoload_functions();
     if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
-        spl_autoload_register();
+        spl_autoload_register('smartyAutoload');
     }
 } else {
     spl_autoload_register('smartyAutoload');

The above fixed it.

I load smarty via my appAutoload function, which is registered by
Code:
spl_autoload_register('appAutoload');
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7366
Location: Lincoln Nebraska, USA

PostPosted: Sun Nov 01, 2009 1:52 am    Post subject: Reply with quote

Smarty tries to use the default spl_autoload() of php if possible for speed, otherwise it falls back to smartyAutoload. What wasn't working?
Back to top
View user's profile Send private message Visit poster's website
mrearl33
Smarty n00b


Joined: 01 Nov 2009
Posts: 2

PostPosted: Sun Nov 01, 2009 10:09 am    Post subject: Reply with quote

Hi, I have the same problem with Zend_Loader from ZendFramework

The problem occurs when an autoloader is already registered but not the default spl_autoload.

I think a better fix is :

Code:

Index: tools/smarty/Smarty.class.php
===================================================================
--- tools/smarty/Smarty.class.php   (révision 3294)
+++ tools/smarty/Smarty.class.php   (copie de travail)
@@ -88,14 +88,17 @@
 * register the class autoloader
 */
 if (set_include_path(SMARTY_SYSPLUGINS_DIR . PATH_SEPARATOR . get_include_path()) !== false) {
-    spl_autoload_extensions('.php,.inc');
     $registeredAutoLoadFunctions = spl_autoload_functions();
-    if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
-        spl_autoload_register();
-    }
+    if(!$registeredAutoLoadFunctions) {
+       spl_autoload_extensions('.inc,.php');
+       spl_autoload_register();
+    } elseif (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
+        spl_autoload_register('spl_autoload');
+    }
 } else {
     spl_autoload_register('smartyAutoload');
-}
+}
+
 /**
 * This is the main Smarty class
 */


I think it can be shorted to :
Code:

if (set_include_path(SMARTY_SYSPLUGINS_DIR . PATH_SEPARATOR . get_include_path()) !== false) {
    $registeredAutoLoadFunctions = spl_autoload_functions();
    if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
        spl_autoload_register('spl_autoload');
    }
} else {
    spl_autoload_register('smartyAutoload');
}


I am using php 5.2.11-1 (from debian sid).

Steeve.
Back to top
View user's profile Send private message
tvdw
Smarty n00b


Joined: 31 Oct 2009
Posts: 3

PostPosted: Sun Nov 01, 2009 2:03 pm    Post subject: Reply with quote

mohrt wrote:
Smarty tries to use the default spl_autoload() of php if possible for speed, otherwise it falls back to smartyAutoload. What wasn't working?
Fatal error because the Smarty_Internal_TemplateBase wasn't found.

And yes, mrearl33 is right, spl_autoload_register() doesn't work if there is already an autoload registered. spl_autoload_register('spl_autoload') also fixes the issue
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7366
Location: Lincoln Nebraska, USA

PostPosted: Sun Nov 01, 2009 4:25 pm    Post subject: Reply with quote

SVN is updated with a fix, please give it a test and let me know if that worked.
Back to top
View user's profile Send private message Visit poster's website
mrearl33
Smarty n00b


Joined: 01 Nov 2009
Posts: 2

PostPosted: Sun Nov 01, 2009 4:32 pm    Post subject: Reply with quote

It works for me

Steeve.
Back to top
View user's profile Send private message
thunderx
Smarty n00b


Joined: 04 Nov 2009
Posts: 2

PostPosted: Wed Nov 04, 2009 11:21 am    Post subject: Reply with quote

Mine is combined with Codeigniter1.7.2,
the SVN doesn't work but mrearl33's short one does Very Happy
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7366
Location: Lincoln Nebraska, USA

PostPosted: Wed Nov 04, 2009 4:57 pm    Post subject: Reply with quote

thunderx wrote:
Mine is combined with Codeigniter1.7.2,
the SVN doesn't work but mrearl33's short one does Very Happy


What error are you seeing? The SVN patch is very similar to that example.
Back to top
View user's profile Send private message Visit poster's website
toma
Smarty Regular


Joined: 25 Apr 2003
Posts: 62

PostPosted: Thu Nov 05, 2009 1:29 am    Post subject: Reply with quote

I just solved a problem I thought was related to this. Using 5.3 I would receive

"Fatal error: Class 'Smarty_Internal_Template' not found in C:\projects\db\library\pear\Text\Diff.php on line 305"

but Smarty_Internal_Template wasn't in there of course. I finally found it was pear's use of references in a class definition near that line:

class Text_Diff_Op_copy extends Text_Diff_Op {
function &reverse()
{
$reverse = new Text_Diff_Op_copy($this->final, $this->orig);
return $reverse;
}
}

Getting rid of the & fixed the not found error.
Back to top
View user's profile Send private message Visit poster's website
toma
Smarty Regular


Joined: 25 Apr 2003
Posts: 62

PostPosted: Thu Nov 05, 2009 2:36 am    Post subject: Reply with quote

Something else to consider on this topic: users may want to use their autoloader instead of adding a second autoloader on the stack. e.g.

Code:

class smartyAutoloader Implements Zend_Loader_Autoloader_Interface {
    public function autoload($class) {
        $_class = strtolower($class);
        if (substr($_class, 0, 16) === 'smarty_internal_') {
            include SMARTY_SYSPLUGINS_DIR . $_class . '.php';
        }
    }
}

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader(new SmartyAutoloader());


I've only just begun to explore autoloading in php but this autoloading newbie thinks using the functionality of my chosen framework is preferable.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7366
Location: Lincoln Nebraska, USA

PostPosted: Thu Nov 05, 2009 2:55 am    Post subject: Reply with quote

You can add multiple autoloaders to the stack.

http://us3.php.net/manual/en/function.spl-autoload-register.php
Back to top
View user's profile Send private message Visit poster's website
thunderx
Smarty n00b


Joined: 04 Nov 2009
Posts: 2

PostPosted: Thu Nov 05, 2009 3:28 am    Post subject: Reply with quote

mohrt wrote:

What error are you seeing? The SVN patch is very similar to that example.

Code:
Uncaught exception 'LogicException' with message 'Class CI_Mysmarty could not be loaded'
Stack trace:
#0 Loader.php(879): spl_autoload('CI_Mysmarty')
#1 Loader.php(829): CI_Loader->_ci_init_class('CI_Mysmarty')
#2 Loader.php(96): CI_Loader->_ci_load_class('Mysmarty', '', NULL, NULL)

It seems that codeigniter loader will add in the prefix CI_ somehow in the middle
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 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