Mike
Mike

Reputation: 3024

Call to a member function createTemplate() on a non-object

I am migrating a website that is based on Smarty and I tried to meet all the prequisites so that there won't be any problems, but ("as allways" I might add) I have this problem where after installing all the necessary packages the website does not work (I have a HTTP 500 Error in the browser) I found this error in the error log:

PHP Fatal error: Call to a member function createTemplate() on a non-object in /var/www/vhosts/placeholder.com/httpdocs/includes/sysplugins/smarty_internal_templatebase.php on line 47

This actually appears from the index.php file where I have this piece of code

$smarty = new SmartyEC($page->template);
$smarty->display('index.tpl');

The problem is with the display of the index template somewhere but I cannot figure out why.

In order to provide more context my constructor looks like this:

<?php

require 'Smarty.class.php';

class SmartyEC extends Smarty{

    function SmartyEC()
    {
        function __construct()  
        {
            parent::__construct();
            $appname ='website';
            $path= Utils::getTemplatesPath();   
            $this->caching = false;

        }       

    }
}

?>

The server has PHP 5.3.2. installed and the latest version of Smarty also installed. I have checked the configuration paths and changed them accordingly and also the file inclusions.

Thank you in advance!

Update #1

I have tried also to remove the function definition like this:

class SmartyEC extends Smarty {
    public function __construct()  
    {
        parent::__construct();
        $appname ='website';
        $path= Utils::getTemplatesPath();   
        $this->caching = false;
    }
}

but the error now becomes:

Uncaught exception 'SmartyException' with message 'Unable to load template file 'index.tpl'' in /var/www/vhosts/website/httpdocs/includes/sysplugins/smarty_internal_templatebase.php:127\nStack trace:\n#0 /var/www/vhosts/website/httpdocs/includes/sysplugins/smarty_internal_templatebase.php(374): Smarty_Internal_TemplateBase->fetch('index.tpl', NULL, NULL, NULL, true)\n#1 /var/www/vhosts/website/httpdocs/index.php(58): Smarty_Internal_TemplateBase->display('index.tpl')\n#2 {main}\n thrown in /var/www/vhosts/website/httpdocs/includes/sysplugins/smarty_internal_templatebase.php on line 127

Update 2

I found this topic CodeIgniter + Smarty = Error that gives the same error but it is not the same situation as here. More intrigueing is the fact that on the other server it works fine so my guess is that there is a configuration glinch rather than a programming issue.

Upvotes: 0

Views: 2430

Answers (2)

Mike
Mike

Reputation: 3024

Finally I got it ... it was a complex of 4 factors that did not work:

  1. I downgraded from smarty 3.1.11 to 3.0.7
  2. I found another config path that was related to the old hosting - got it fixed easy
  3. The .tpl files contained php code like "{php} some code here {/php}" and got it fixed with $this->allow_php_tag = true; inside the constructor function
  4. Even though I have set the correct permissions the system could not rewrite the compile folder therefore I deleted everything in the compile folder.

Upvotes: 0

rodneyrehm
rodneyrehm

Reputation: 13557

Are you sure about the nesting of __construct() within SmartyEC()? (rhetoric question, sorry)

Had you explicitly named your functions public, the error would have immediately surfaced:

class SmartyEC extends Smarty {
    public function SmartyEC()
    {
        public function __construct()  
        {
            parent::__construct();
            $appname ='website';
            $path= Utils::getTemplatesPath();   
            $this->caching = false;
        }
    }
}

gives you

Parse error: syntax error, unexpected T_PUBLIC in test.php on line 7

As of PHP5 we don't use class-name-constructors anymore. We use __construct(). Unless you call $ec = new SmartyEC(); $ec->SmartyEC(); explicitly somewhere, that function declaration should be removed:

class SmartyEC extends Smarty {
    public function __construct()  
    {
        parent::__construct();
        $appname ='website';
        $path= Utils::getTemplatesPath();   
        $this->caching = false;
    }
}

please also note that your example invocation $smarty = new SmartyEC($page->template); passes an argument - one that neither SmartyEC() nor __construct() expected.

Upvotes: 1

Related Questions