Page 1 of 1

how to call UDT into another UDT

Posted: Sat Dec 28, 2013 1:29 am
by uloloi
Hi, is it possible?. I want to call simple functions located into an UDT and have to be used it in another UDT, like a Class.

Or how can I use recursive functions?

Re: how to call UDT into another UDT

Posted: Sat Dec 28, 2013 1:55 am
by calguy1000
Is it possible: YES (though not easily)
Is it advisable: NO

A UDT is a simple PHP function (the code of which are stored in the database and executed using 'eval'. They are a means by which you can create simple PHP event handlers, and simple functions to handle simple tasks without having to have a text editor. They are primarily used for CMSMS event handlers. UDT's are not intended to, and we will advise against using them as a 'code library'.

While you can define multiple functions inside a PHP function, the child functions are not callable until the parent is called.

i.e:

Code: Select all

function a() {
  function b() { echo 'foo '; }
  function c() { echo 'bar '; }
}
functions b(), and c() will not exist until function a() is called, after which functions b() and c() will exist in global scope. you can assume that your UDT is like function a() (but with a much different name).

If you want to build a library of functions, or classes.. then do so in the normal way, with static files. You can call your code from within a UDT all you need is the appropriate require_once($filename); statement.

Note: You cannot use relative paths in the $filename parameter. You will have to build the $filename variable dynamically using the config values, or hardcode the value (again not advisable). This is because the 'current working directory' values are different when CMSMS is handling admin or frontend actions.

i.e: if your class/function file is located in <root>/lib/myfiles/something.php

you should use:

Code: Select all

$config = cmsms()->GetConfig();
$filename = cms_join_path($config['root_path'],'lib','myfiles','something.php');
require_once($filename);

Re: how to call UDT into another UDT

Posted: Sat Dec 28, 2013 8:10 am
by Rolf
It has another purpose, but it might help https://www.cmscanbesimple.org/blog/cal ... from-a-udt

Re: how to call UDT into another UDT

Posted: Sat Dec 28, 2013 5:39 pm
by uloloi
wow! these are big help. Now I have two very good options, and help me to save time

I think, like Calguy says, in most of cases, my best solution should be to use lib/classes, something like this:

Code: Select all

$config ...;
$filename = ...;
require_once($filename);
// UDT:test, p.e. using Classes.php from /lib
$me = new MyClass();
$me->displayVar('Something here...  from UDT');
Amazing!... simple and clear.

However, doing a couple of test using modules and tags inside of UDTs like Rolf's help is confortable. I did this:

Code: Select all

// plugins/function.test.php
function smarty_function_test($params, &$template)
{
  if(isset($params) && count($params) > 0){
    if(isset($params['param2'])){
	  $params['param2'] = do_some($params['param2']);
	}
  $listparams = ' + Yes params ('.print_r($params).')';
  }else{
  $listparams = '';
  }
  
  $hi = "Hello World!".$listparams;
  return $hi;
}

function do_some($str){
  $go = "running function do_some('$str')";
  return $go;
}
And then the UDT:
$txt = "{test}"; // calling the plugin
$smarty->display('eval:'.$txt);

$heredoc = $smarty->fetch('eval:{test param1=1 param2="my string" param3=array(1,2,"c")}');
echo "...Now I'm in the UDT, its \$heredoc = $heredoc";


I hope the practice or sintax must be the appropiate, I mean, been into the good practices, I don't know if call a smarty->display|fetch instruction is correct

Men, many thanks for the tips