How can I automatically add a namespace to a new PHP class in Netbeans 7.0

I can edit the PHP class template using Tools> Template Manager and then PHP Class> Edit. My project structure supports autoload 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 this?

And as a side, how can I find out which variables are available to me in the template?

+8
php netbeans
source share
3 answers

UPDATE: It was fast! I admit that testing is very bad here, but it works if you use New -> PHP Class... through the user interface. Then you select the namespace from the drop-down list. And voila. I used New -> PHP File... which does not provide a drop-down namespace space (oddly enough, or maybe I missed something). Customize the PHP class template if necessary. Sorting criterion.


I scratched my head about this too and found the following error report . In accordance with the instructions (at the bottom), you edit the template file and add:

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

Netbeans then makes guest access based on the location of the files and other possible namespaces in the directory. To be honest, I still managed to do this, but I'm sure I'm not far off ... maybe ... U_U

I will definitely update this if so.


SIDE Note . You can add any variables that you want to use in the template. Go to (on windows) \AppData\Roaming\NetBeans\7.4[your version here]\config\Templates\Properties\User.properties (create it if it is missing)

Then create something like this:

 user=Ms Angelina Jolie Lookalike Honest <no.spam.thanks@gmail.com> organization=the.Evolution.of.Awesome package=SunshineInACan package2=Framework/UserInterface 

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 little time is to set common parameters in one variable and just delete as applicable after creating the file. For example, ${package2} is either "Framework" or "UserInterface" not both (or both, if I forgot, you know how this happens).

+2
source share

I remember editing the default class template that caused some problems, and I created a new class template for my needs. You can do this by selecting an existing class file and running Save As Template from the context menu

Regarding the 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 links:

http://blogs.oracle.com/netbeansphp/entry/how_to_manage_templates_in

http://blogs.oracle.com/netbeansphp/entry/how_to_manage_templates_in1

0
source share

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

0
source share

All Articles