how to call UDT into another UDT

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
uloloi
Forum Members
Forum Members
Posts: 88
Joined: Thu Dec 16, 2010 8:50 pm

how to call UDT into another UDT

Post 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?
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: how to call UDT into another UDT

Post 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);
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: how to call UDT into another UDT

Post by Rolf »

It has another purpose, but it might help https://www.cmscanbesimple.org/blog/cal ... from-a-udt
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
uloloi
Forum Members
Forum Members
Posts: 88
Joined: Thu Dec 16, 2010 8:50 pm

Re: how to call UDT into another UDT

Post 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
Post Reply

Return to “The Lounge”