[solved] UDT: are PHP namespaces allowed?

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
Cerulean
Forum Members
Forum Members
Posts: 172
Joined: Mon Nov 01, 2010 8:56 am

[solved] UDT: are PHP namespaces allowed?

Post 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?
Last edited by Cerulean on Sun Dec 14, 2014 12:42 am, edited 1 time in total.
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: UDT: are PHP namespaces allowed?

Post 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.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: UDT: are PHP namespaces allowed?

Post 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.
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.
Cerulean
Forum Members
Forum Members
Posts: 172
Joined: Mon Nov 01, 2010 8:56 am

Re: UDT: are PHP namespaces allowed?

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

Re: UDT: are PHP namespaces allowed?

Post by calguy1000 »

Then it is not a problem with CMSMS, but something specific about that autoloader.
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.
Post Reply

Return to “The Lounge”