Page 1 of 1

[solved] UDT: are PHP namespaces allowed?

Posted: Fri Dec 12, 2014 3:48 am
by Cerulean
I'm trying to get the Gregwar/Image PHP class working on my CMSMS site.

I can use the class okay directly in a PHP file, but when I try and use the same code in a UDT I get an error in my log:

Code: Select all

PHP Fatal error: Call to undefined function cms_user_tag_gw_image()
My testing code is...

Code: Select all

require_once('vendor/autoload.php');
use Gregwar\Image\Image;
echo Image::open('demo_image.jpg')->zoomCrop(300, 300)->jpeg();
The UDT seems to fail at the "use Gregwar\Image\Image" line, so I'm wondering: can namespaces be used in a UDT?

Re: UDT: are PHP namespaces allowed?

Posted: Fri Dec 12, 2014 5:22 pm
by JohnnyB
I don't know if it can be done in an UDT only because I don't know how much of the smarty global will be needed which I think is no longer allowed in UDT, correct?

But, maybe this helps and maybe it will have to be a plugin instead ?
You can register a class with optional namespace for the use in the template like:
$smarty->register->templateClass('foo','name\name2\myclass');
In the template you can use it like this:
{foo::method()} etc.

Re: UDT: are PHP namespaces allowed?

Posted: Fri Dec 12, 2014 6:14 pm
by calguy1000
Don't use relative paths.
The cwd is different in the admin console than it is in the frontend.

Use the CMSMS config variables, and build an absolute path.

Re: UDT: are PHP namespaces allowed?

Posted: Fri Dec 12, 2014 10:22 pm
by Cerulean
Thanks for the replies.
You can register a class with optional namespace for the use in the template
I saw this in the Smarty documentation too, but I don't think it applies in my situation - I don't need to use the namespace in my template, only inside the UDT.
maybe it will have to be a plugin instead
Tried this and it works! I don't understand why though - I thought anything that could be executed in a plugin could be executed in a UDT. Are there additional restrictions to the PHP that can be executed in a UDT?
Anyway, delighted to have it working.
Use the CMSMS config variables, and build an absolute path.
The require_once line is working fine - the UDT seems to fail at the "use Gregwar\Image\Image" line.
I changed to using an absolute path in the require line but the UDT still fails.

Re: UDT: are PHP namespaces allowed?

Posted: Fri Dec 12, 2014 10:41 pm
by calguy1000
Then it is not a problem with CMSMS, but something specific about that autoloader.