'How to add object to smarty without error: undefined extension class 'Smarty_Internal_Method_Register_Object'

I added Smarty 3.1.33 to my project with composer. The basic functionality works fine, but now to my I want to add an object to Smarty. For this I follow the documentation and have this code:

   class My_Object {
     function meth1($params, &$smarty_obj) {
   return 'this is my meth1';
      }
    }
    $myobj = new My_Object;

    require_once __DIR__.'/vendor/autoload.php';
    $smarty = new Smarty;
    $smarty->register_object('foobar',$myobj);

Very basic according to the documentation, but the result is this:

Notice: Undefined property: Smarty_Internal_Undefined::$objMap in /home/svn/test1/trunk/smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_extension_handler.php on line 132

Fatal error: Uncaught --> Smarty: undefined extension class 'Smarty_Internal_Method_Register_Object' <-- thrown in /home/svn/test1/trunk/smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_undefined.php on line 62

I cannot find anything on the web about this, so am I the only one who ran into this issue? I hope someone here can help me out so I do not have to start debugging Smarty.

Thanks!



Solution 1:[1]

I hat a similar Problem when switching from Smarty 2.x to 3.x with the error:

Notice: Undefined property: Smarty_Internal_Undefined::$objMap in smarty\sysplugins\smarty_internal_extension_handler.php on line 132
Fatal error: Uncaught --> Smarty: undefined extension class 'Smarty_Internal_Method_Register_Resource' <-- thrown in smarty\sysplugins\smarty_internal_undefined.php on line 62

The reason was you musst change register_object to registerObject and register_resource to registerResource. Thats all.

So in your case replace the line:

$smarty->register_object('foobar',$myobj);

with

$smarty->registerObject('foobar',$myobj);

Solution 2:[2]

I guess you should add the invocation () parenthesis beside each constructor (i.e.

$smarty = new Smarty();

$myObj = new MyObject();

Solution 3:[3]

There was one answer that you should:

The reason was you musst change register_object to registerObject and register_resource to registerResource. Thats all.

Sorry but that is not all ... I've had the same problem and I needed to change:

  • register_object --> registerObject
  • register_resource --> registerResource
  • get_config_vars --> getConfigVars
  • config_load --> configLoad
  • get_template_vars --> getTemplateVars

I guess there could be more - those are the ones that I've used for my old project.

Solution 4:[4]

Simply include <path to Smarty>/libs/SmartyBC.class.php instead of <path to Smarty>/libs/Smarty.class.php in your project. It represents a backward compatibility wrapper class with the old function names.

See Chapter 19. SmartyBC - Backwards Compatibility Wrapper for example.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Radon8472
Solution 2 dbc
Solution 3 Konki
Solution 4