2

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!

Ted
  • 83
  • 1
  • 10

4 Answers4

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);
Radon8472
  • 4,285
  • 1
  • 33
  • 41
0

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.

Konki
  • 186
  • 1
  • 4
  • This list is probably better than the one in the accepted answer. But applying your replacements still did not solve my problem. Maybe it's "there could be more". But that raises the question: How did you find these? (I haven't been using Smarty since 2007.) – steffen Nov 11 '22 at 19:57
  • I created a file with all setters containing an `_` from the `class Smarty` and searched for these in my source. `while read line; do "grep $line"; grep -risl $line src | grep -v third-party; done < functions`. This helped me discovering that `register_outputfilter($callback)` needs to be replaced by `registerFilter('output', $callback));` with the help of Smarty man pages. – steffen Nov 11 '22 at 20:40
0

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.

ltlBeBoy
  • 1,242
  • 16
  • 23
-1

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

$smarty = new Smarty();

$myObj = new MyObject();
dbc
  • 104,963
  • 20
  • 228
  • 340
  • I (and PSR) agree on that, but this has nothing to do with the problem. Also, PHP documentation writers do omit parens. – steffen Nov 11 '22 at 19:57