1. Trang chủ
  2. » Công Nghệ Thông Tin

Publishing PHP eclipse - part 7 pot

10 132 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 315,35 KB

Nội dung

Writing PHP Code with Eclipse The first screen of this wizard allows you to select the type of application to create. All but one of these application types are available in the standard Eclipse SDK. The lone exception is the PHP Project , which is available courtesy of PHPEclipse. Since we're creating a PHP web application, click on PHP Project and then the Next button. 60 Chapter 4 This screen will require us to give a name to the application. The second part asks us if we would like to store the files in the default workspace. If the Use default checkbox is checked, Eclipse will automatically create a directory of the same name under the workspace. If the Use default box is not checked, we can browse to another location to store the files in the file system. This is one of the methods to override the default workspace. Give the application a name and click on the Finish button. When this is done, your application will be listed in the Navigator view as a project. Underneath the project are the files that belong to the application. Right now, it is empty except for a .project file. This file is used by Eclipse to keep track of projects and their interactions with Eclipse. Adding Directories We will add a few subdirectories and source code files to the project in order to build our application. To create a new directory, select File | New | Folder from the main menu: In this example, we are creating a subdirectory named classes to store our PHP classes. Click on the name of a project in which you wish to create a subdirectory. In this case, it would be the ShelterSite project. Enter the name of the subdirectory in the Folder name field. We are simply going to call the subdirectory classes. When you click on Finish, this will create a directory under the project workspace in both the file system and Navigator view. If you click on the Advanced button, this will give you access to a powerful file mapping feature of Eclipse. This feature will allow you to bring other areas of your file system into your project. 61 Writing PHP Code with Eclipse By clicking on the Link to folder in the file system checkbox, you can add a directory that lies outside a project's workspace either by typing in the full path of the directory or browsing with the Browse… button. Further, you can define variable shortcuts to use in this field by clicking on the Variables… button. This will bring up the Select Path Variable window: In this example, we have two example variables defined. The app variable will link to this computer's Applications directory and home will link to my home directory. Clicking on New… will bring up a dialog box that asks for two parameters—the name (or shortcut) you wish to assign and the directory that the shortcut maps to. Once you create a mapping name, whenever you type this name in the Folder name field of the New Folder dialog box, it will go directly to this directory. You can use this as a shorthand device for more complicated paths, or it can be used as a mapping to resources that lie outside a project's default workspace. After you create a directory, it will appear underneath the project in the Navigator view. Here, we have created two other directories under ShelterSite—one for images and one for styles. 62 Chapter 4 Creating PHP Files As expected for a Java IDE, the standard Eclipse download easily lets you create files geared towards Java development. Templates and wizards to create things like classes, interfaces, and packages are easily accessible. PHPEclipse expands on this and helps you easily create PHP and HTML files. In this example, we will create a PHP object class for the ShelterSite project. To create a PHP or HTML file, select the directory where you want the file to go in the Navigator view. Go to the File | New | PHP File… option. This will bring up the PHP New File wizard. The Container field is the project and subdirectory under which the file will be created. This is the same location as you clicked on at the start. You do not have to modify this field if you selected the right directory in the Navigator view. However, you can use this field to place a file outside of the workspace. Give the file a name in the File name field. Click on the Finish button and Eclipse will create this file. You can also create PHP files, as well as other file types, by selecting the File | New | Other… option instead of PHP File…This will bring up the New File wizard. 63 Writing PHP Code with Eclipse In this wizard, we can select the source file type of any language or project we are working in. Highlight the file type and click on the Next button. The screens thereafter will ask for information specific to the project type or language. Code Templates Notice that Eclipse automatically generates a comment block every time you create a PHP file. If you create an HTML file, you'll notice that Eclipse automatically includes standard tags such as <DOCTYPE> and <HTML> in this file. If your company has a standard template for source code files, you can customize these templates to fit your requirements. You can add things like the creation date and your name. PHPEclipse adopts a powerful template feature of the Java IDE to build these code blocks. To edit these templates, go to Window | Preferences… | PHPEclipse Web Development | PHP | Code Templates in the main menu. This will bring up the code templates Preferences. 64 Chapter 4 Select the template for the type of file you wish to edit—PHP or HTML. You can preview the template in the Pattern field. Click on the Edit… button to edit the template in the Edit Template dialog box. You can manually edit the template from here. A number of auto-insert variables are available in the Insert Variable button. The code template feature is borrowed from the JDT. Therefore, some variables, like package name, are not relevant for PHP development. However, most variables are useful such as the date and time, to-do tasks, file name, user name, and project name. 65 Writing PHP Code with Eclipse 66 Variable Code Description Date ${date} Date of file creation. Dollar Sign $$ Since dollar signs are used to indicate a variable name in the template, to add a dollar sign in a template, use two dollar signs. File Name ${file_name} The name of this file. Package Declaration ${package_declarati on} Not implemented by PHPEclipse. Package Name ${package_name} Not implemented by PHPEclipse. Project Name ${project_name} Not implemented by PHPEclipse. Time ${time} Time of file creation. Todo ${todo} Adds a to-do item to the Task view. Type Declaration ${type_declaration} Not implemented by PHPEclipse. Type Name ${type_name} Not implemented by PHPEclipse. Type Comment ${typecomment} Not implemented by PHPEclipse. User ${user} The account user name on the local machine. Year ${year} Year of file creation. The JDT code template preferences can be changed by selecting Window | Preferences | Java | Code Style | Code Templates from the main menu. These preferences include many more comment options and allow you to control the code that is automatically created by the JDT. When you have finished editing your templates, you can share them with co-workers and team members by using the Export… and Export All… buttons in the code template's Preferences. The exported files will be XML files mapped to the PHPEclipse plug-in. These files can be easily exchanged between team members. For example, you can place these files on a network server for new employees to download. On the receiving workstations, use the Import… button to integrate the templates with the local copy of Eclipse. This helps ensure that everyone is using the same standard when coding. The Editor We will be doing all of our code writing in the editor, so it will help to be familiar with how the editor works and the feedback it gives us. On the surface, the Eclipse editor looks and acts like a standard text editor. Once we start coding, we will see many time-saving features. Among these are the visual clues Eclipse conveys. Eclipse relies on syntax coloring and icons to communicate information on our work. We can quickly process information on problems and navigate around source code. Chapter 4 Editor Visual Aids As you type, the Eclipse editor marks the syntax of your code with certain colors. When you save, Eclipse reviews your code, marks potential pitfalls, and alerts you via icons in the margin. The screenshot above shows a typical editor with a few lines of sample code and some problems. Let's first take a look at the left margin. 67 Writing PHP Code with Eclipse Line 5: Warning The keyword var is deprecated in PHP 5, but will not cause a runtime error. Syntax problems like this that will not cause the program to fail will be automatically flagged with this warning icon. Line 7: To-do This is a note to yourself of something that should be addressed. Eclipse maintains a set of to-do items for you in the Tasks view. To-do items are memos and notes to you within the code. We'll see how to use to-do items later. Line 9: Error Errors are problems in the code that will cause the program to fail. In this case, line 8 is missing a semicolon. Eclipse sees the new line at line 9 and tells you that Eclipse was suggesting a semicolon on the previous line. Error icons are automatically added by Eclipse. Line 12: Bookmark Bookmarks are placeholders in the code. Chapter 3 covered adding bookmarks from the Edit menu and use of the Bookmarks view. You can also add a bookmark by right-clicking on the margin and selecting Add Bookmark… in the contextual menu. Line 14: Breakpoint Breakpoints are used to stop code execution during debugging. We will cover debugging in Chapter 5. Breakpoints tell the IDE to give you a snapshot of variable values and program state up to that particular point. To add a breakpoint, right-click on the margin where you want to stop and select Toggle PHP Breakpoint. For any of these left-margin icons, you can hover your mouse cursor over the icon to get the exact message or error that Eclipse is conveying. In the right margin is the overview ruler. The overview ruler shows all of the left-margin icons in the page. The overview ruler is scaled to the entire page so that you will always see all of the issues on a page in the overview ruler. Clicking on any of these rectangles in the right margin will take you to the exact line with the marker. All of the items in the overview ruler are also represented in an appropriate view. For example, bookmarks in the overview ruler also have a corresponding item in the Bookmarks view, and problems will be listed in the Problems view. At the top of the editor, each open file is represented by a tab. The tab will have the same icon for the file as in the Navigator view and this icon depends on the file type and the name of the file. The tab may have other symbols on it depending on the state of the file. An asterisk is a visual notice that the file has been changed, but not saved: As in the left margin in the editor, the yellow caution icon tells you that there is a non-fatal error in the code: Again, as in the left margin in the editor, the red error icon tells you that there is a fatal error in the code of this page. Fatal error icons take precedence over warning icons. A page with a fatal error icon may also have a warning in the code. 68 Chapter 4 A tab with just the icon and name tells you that there is nothing wrong with the code: Customizing the Editor PHPEclipse's editor can be customized to your own work style in its own set of preferences. These preferences are located in the Window | Preferences… | PHPEclipse Web Development | PHP menu option. The Appearance tab controls most of the editor's higher-level behaviors. The overview ruler can be turned on and off here. You will probably want to turn on line numbers as soon as you get started with Eclipse as this feature is turned off by default. Check the Show line numbers checkbox in this tab and line numbers will appear in the left margin of the editor. Other editor behaviors that you can toggle include highlighting the matching curly bracket when your cursor is next to a curly bracket, highlighting the current line you are on, and showing the print margin of a page of code. You can change the color of all of these highlighting options in the Appearance color options section of this tab. 69 . ${package_declarati on} Not implemented by PHPEclipse. Package Name ${package_name} Not implemented by PHPEclipse. Project Name ${project_name} Not implemented by PHPEclipse. Time ${time} Time of. accessible. PHPEclipse expands on this and helps you easily create PHP and HTML files. In this example, we will create a PHP object class for the ShelterSite project. To create a PHP or HTML. your name. PHPEclipse adopts a powerful template feature of the Java IDE to build these code blocks. To edit these templates, go to Window | Preferences… | PHPEclipse Web Development | PHP | Code

Ngày đăng: 04/07/2014, 17:20

TỪ KHÓA LIÊN QUAN