Home » Php » How can i automatically add a namespace to new PHP Class in Netbeans 7.0

How can i automatically add a namespace to new PHP Class in Netbeans 7.0

Posted by: admin July 12, 2020 Leave a comment


I can edit a PHP Class template through ‘Tools’ > ‘Template Manager’, and then ‘PHP Class’ > ‘Edit’. My project’s framework supports autoloading using namespaces, so i want to automatically add a namespace to the top of the file whenever i create a new ‘PHP Class’. How can i do that?

And as a side, how can i find out what variables are available to me in a template?

How to&Answers:

UPDATE: That was quick! I will admit to very shoddy testing here, but it works if you use New -> PHP Class... through the UI. Then you select the namespace in the drop-down. Et voila. I was using New -> PHP File... which does not provide the namespace drop-down (oddly, or maybe I just missed something). Customise the PHP Class template as needed. Sorted.

I have been scratching my head about this too and found the following bug report. According to the instructions (towards the bottom), you edit the template file and add:

<#if namespace?? && namespace?length &gt; 0>
namespace ${namespace};

Then Netbeans makes a guestimate based on the files location and other possible namespaces in the directory. To be honest though, I have quite managed to make it work yet but I’m sure I’m not far off… probably… U_U

I’ll be sure to update this if I do.

SIDE NOTE: You can add any variables you want to a template. Go to (on windows) \AppData\Roaming\NetBeans\7.4[your version here]\config\Templates\Properties\User.properties (create it if it’s missing)

Then create something like this:

user=Ms Angelina Jolie Lookalike Honest <[email protected]>

Then in your template do something like this:

 * @DNADO   Type description
 * @package     ${package}.${package2}
 * @subpackage
 * @category
 * @author      ${user}
 * @copyright   ${date?date?string("yyyy")} ${organization}

Then… disco 😉

What I also do to save a bit of time is put common options in one variable and just delete as applicable after I create the file. For example ${package2} is either ‘Framework’ or ‘UserInterface’ not both (or both if I forget, you know how it goes).


I remember editing default class template causing some problems and I ended up creating new class template for my needs. You can do it by selecting existing class file and running ‘save as template’ from context menu

As for available variables:

Variable Name  Description
 name           contains the name of the file that is being created
 user           contains the user name
 nameAndExt     contains the name and extension of the file that is being created
 date           contains text representing the current day like 23. 3. 2007
 time           contains text the current time like 17:18:30
 encoding       the file encoding of the template instance

Useful references:




namespace include.class you can use the classes same as include and requires funciton