Archive for the 'php4' Category

Separating the Layers Now that we have a

Tuesday, January 30th, 2007

Separating the Layers Now that we have a good idea about multi-tier development, it’s time to see how we can achieve it. Basically the task is to separate the identified layers in your application and minimize the interaction between them. Why? Because when you change a layer, the changes reflected to the other layers are proportional to the level of contact between the layers. If the layers are very strongly coupled, changing a layer would usually imply changing the other too. Since changing layers implies programming, we want to minimize the number of hours/lines of coding we need when we change a system. This is a really practical definition of software maintainability. To separate the layers abstractions must be built. Each layer must have a programming abstraction representing it: object-oriented programming, design patterns, and a careful design are the best tools to achieve this task. Minimizing the interaction between layers becomes a must if performance and flexibility over scale are to be retained. A multi-tiered development process should have at least the following goals: . Maintainability . Modular programming . Independence between logic and presentation . Independence between logic and content . Portability . Database independence . Code reusability We will discuss some of the above topics below. Modular Programming Modular programming is important, as you cannot easily separate the different layers we have identified without programming in a highly modular way. Modular programming will lead to isolated and encapsulated modules for each logic element in the system. OOP is the best method to increase modularity in PHP code, as it promotes unit and integration testing, the use of XP methodology, the use of design patterns with OO, and reusability. Also, you should stress code reusability. If you achieve this goal, software will be easier to maintain and you will be able to code less in future applications. Independence between Logic and Presentation Separating logic and presentation is the key to highly maintainable software. The idea is simple if the designers intend to change the presentation of the site, no logic should be modified, and if the editors change the content, no change should be reflected in the logic of an application. Independence between Logic and Content If the editors change the content no change should be seen in the logic of the application. Database Independence If you use a database you don’t have to depend on the database; it must be easy to change the DBMS being used without changing the logic layer of the application. Page 537
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services

depending on how step 1 was performed. See

Tuesday, January 30th, 2007

depending on how step 1 was performed. See Chapter 21 for an example of XML processing. . XSL Rendering Applying an XSL style sheet renders the document generated in step 2. Specific formatting is applied, resulting in a document applicable to the requesting client. Possible outputs are HTML, PDF, WML, XML, and others. Page 536
Note: If you are looking for inexpensive but high quality provider to host and run your serlvet application check Astra servlet hosting services

The data presentation component uses the presentation language

Tuesday, January 30th, 2007

The data presentation component uses the presentation language component to build layouts and designs for the data. This is the component where you put all the functions needed to render the data. Presentation Languages There are a lot of new presentation languages available that can display pages on browsers and even voice-enabled languages for the phone. This is a short summary of a few presentation languages: . HTML HTML 4.1 is indeed a presentation language. A lot of fixes had been applied to the language to modularize many presentation aspects, mainly involving the use of CSS. HTML with CSS is a popular presentation language and the one that most browsers use today, so it has a lot of importance. If you are going to use HTML as a presentation language we recommend reading the W3C (http://www.w3c.org/) recommendations and use CSS to abstract the style from the content in HTML pages. . XHTML XHTML is an XML-compliant version of HTML by the W3C. It is very similar to HTML but more restrictive: the file must obey XML formatting rules. It is a very good idea to use XHTML instead of HTML for new applications; as it is probable that XHTML will replace HTML in a near future. . HDML HDML is a reduced subset of HTML for handheld devices and PDAs. While some devices can display HTML, there’re a lot of restrictions that apply to the kind of content that a hand held can display. . WML WML is an XML vocabulary for presentation of content in mobile devices, usually phones. WML has features such as forms, paragraphs and tags that a WML-enabled cell phone can display. Most WAP applications today use WML as their presentation language (Chapter 16 contains a multi-tier WML application). . SVG SVG is an XML vocabulary to display graphics. A set of tags can be used to display geometric figures such as circles, rectangles, text, lines, and so on. Graphics are defined in XML in a vectored way (JPG, GIF, and BMP are bitmaps). There are plugins available for browsers and specific tools to display SVG files, SVG graphics can be of great quality and they have many interesting features such as non-deforming zoom (because the graph is vectorial), a scripting language to provide animation features, searchable text in graphic files and so on. You can learn more about SVG from the W3C site (http://www.w3.org/). . VOICE XML Voice XML is yet another presentation language using an XML vocabulary. It is used to build voice enabled sites and applications. An XML vocabulary can be used to voice phrases, accept DTMF codes, and so on. You can visit the W3C site to and find out more about Voice XML. If your web site or application will have to use a huge number of presentation languages then you may find an XML based architecture useful. We will cover XML based architectures later in the chapter. XML-Based Architecture In an XML-based architecture, the idea is that the logic layer of the application will generate XML data, which can be transformed using XSLT to any presentation language that you need: The XML web publishing framework separates web content generation into three different steps: . XML Creation Content owners create and develop the XML file(s). The content owners do not need to know anything about how their content is processed or presented. In our applications, the business logic layer will be responsible for generating the XML content. Some database vendors are enabling their databases to produce XML data directly out of SELECT statements. . XML Processing The XML file is processed. Any intermediate logic is applied here. This step may be eliminated Page 535

Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Sandzak.com tomcat web hosting provider

Architectures for Multi-Tier Development The various multi-tier development

Tuesday, January 30th, 2007

Architectures for Multi-Tier Development The various multi-tier development architectures are: . HTML-based architecture . XML-based architecture HTML-Based Architecture HTML-based architectures map to the architecture as shown below: In the diagram you can see an approximation of a multi-tiered architecture. The three layers are visible and we’ve also pictured the abstraction for the presentation language and different storage options available. Alternatively, you could build a database cluster at the back end for your databases. The database then has to be enabled to work among the cluster of servers. The Content Layer The content layer abstracts the data modeling language being used. There are usually two different components in this layer: . The Access Component . The Data Manipulation Component The Access Component The access component abstracts the storage method being used and provides basic primitives for storing, retrieving, and manipulating data. If you use files you will not have an access component. If you use XML then you can have an access component to store and retrieve XML files so that you can easily use a file system, a repository, or the Web to store XML files without changing to other modules. If you use databases then the access component is a class abstracting the database being used. It provides functions to connect, query, and process results from the database. If you decide to change your database then all you need to do is to change this class without modifying the code that uses it. The Data Manipulation Component The data manipulation component is a client of the access component. This is where you will place all the functions needed to manage the data model, get data, store data, delete data, update data, insert data, and so on. The data manipulation component also handles the integrity and referential integrity of the data. Modules in this layer are not responsible for checking data to be consistent with the logic of the application since this will be handled at the logic layer. The Logic Layer The logic layer will be represented by a set of classes, each encapsulating different business rules or logical components. The functionality of the whole system will be coded in this layer. The logic layer uses the content layer to retrieve data to be manipulated. It will query the data model and update according to the logic specific to the application. The logic layer also uses the presentation layer to present the data after it has been manipulated. The Presentation Layer In the presentation layer you will find all the functions needed to present data to the client. You can divide this layer into two components: . The presentation language component This component encapsulates presentation functions according to the presentation language being used, say HTML. When an abstract presentation feature is needed, you just add it to the presentation language component. . The data presentation component Page 534

Note: If you are looking for good and affordable webspace to host and run your servlet application check Sandzak servlet hosting services

manipulation, such as getting the current poll, adding

Tuesday, January 30th, 2007

manipulation, such as getting the current poll, adding a poll, voting, getting the options from a poll, and so on. Then, if we decide to add forums to our site or application, we can design and create a new class/module that has absolutely no relationship or dependence on objects before and after it. The Presentation Layer In the presentation layer you add design and layout elements to the content prepared in the logic layer. This is where you generate HTML using CSS, Flash, images, and whatever else design experts want to use to make the application attractive. Also, client-side code or presentation layer plug-ins give the presentation layer increasing power to share the computing load of the application proper. The Explosion of Web Devices In the beginning, only browsers accessed the web and they were limited in their functionality. Now we have web-enabled devices such as cell phones, pagers, e-mail clients, PDAs, hand helds, POS terminals, data capturing devices, and more. In the near future even small appliances such as microwave ovens and freezers may want to access the web to get data or publish information about their state. Different devices require different presentation languages. We can have HTML, XHTML, XML vocabularies, WML, and other presentation languages. If there are two devices accepting the same presentation language it is clear that they might require a different kind of content. We cannot compare the screen of a modern PDA with a text display in a fridge. So there will be a requirement for different presentation languages and formats from a dozen different devices accessing a web application. Don’t forget machines, programs accessing the web collecting information. We might have to generate a presentation language for these programs, usually an XML vocabulary or similar. This will enable the creation of web services where organizations provide services and use others to create complex distributed web applications. While the classic multi-tiered architecture is very useful to separate different layers it is certainly oriented towards HTML-based web applications and sites. We can change the presentation language but this usually implies a lot of effort since we have to recode a lot of functions, some almost impossible in the new presentation language. Sometimes there will be no mapping between an HTML function and the presentation language we want to use. If this happens then there’s something wrong: we are using HTML as our base language. We have built an HTML class and are trying to adapt it to other presentation languages. We tend to force every presentation language to be mapped to HTML and that’s impossible. The best solution to the problem of dealing with a lot of different presentation languages seems to be the use of XML. Page 533
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra j2ee hosting services

6 green 7 I really like blue As

Tuesday, January 30th, 2007

6 I really like blue
As you can see here, the poll, option, and votes elements can all be stored in a single XML file. Let’s assume we did not consider that a user might want to add comments to polls. To include the comments in the relational model, we would have to create a table for the comments. In XML we just add comment elements to the XML file. The really big advantages of XML over a relational model are the following: . SQL has a proprietary model for metadata while XML uses an open standard. . SQL is more or less a standard, but the way in which the data is internally stored is proprietary to each DBMS. . You cannot take a table and understand content without the DBMS interpretation and reporting facilities. . XML is designed for data interchange while the relational model has to bolt on transform tools. . You cannot send a proprietary SQL table in open code form. With XML, you can send simple XML files. Any transformation problems are also addressed by the open standard of XSLT. Finally, to implement an XML-based data model in PHP you must first define how, where, and why to store the data stream. You could use plain files somewhere in the file system or you could use an XML repository solution, like Ozone or dbXML (http://www.dbxml.org/). Chapter 21 has more in-depth coverage of PHP’s XML APIs. There we cover reading, writing, and transforming. Hybrid Model The hybrid data model combines two or more different data modeling strategies. For example, you can have a relational model and a set of XML DTDs and files co-existing in the same application. Hybrid models add complexity to the content layer since there will be more than one interface to store and retrieve data. Although hybrid models demand high levels of design skill at the planning stage, they are the most flexible, scalable, and useful in today’s e-business world. The Logic Layer The logic layer is where you find all the intelligence of an application. In this layer you manipulate data pulled from the content layer and prepare it. Data manipulation like calculations, transformations, statistical information, security, and audit pathing are all set by the logic tier. User tracking systems, logging systems, caching systems, and many others are found at the heart of this layer. The most important consideration about the logic layer in PHP is to design it in a modular way. You can design separate classes/functions for the different business rules or functions that the application demands. In our polls example, we could create a Polls class where we encapsulate all the methods needed for Page 532
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check professional tomcat hosting services

This structure is similar to the plain file

Tuesday, January 30th, 2007

This structure is similar to the plain file model except that tables replace files. We don’t have to bother about the internal structure of tables since this is what a Database Management System (DBMS) is for. We don’t have to update or delete each entity, since we do that with SQL statements. DBMS gives these common advantages: . Higher integrity of data (not guaranteed with files) . Higher consistency of data using multiple access . Higher security . Common query language . Different views using same structures for multiple uses . Independence of file structures . No redundancy of information . Relational mapping with OO . Less hard drive space from loss-less joins And these disadvantages: . DBMSs are slower than files . DBMSs require additional software . Commercial DBMSs may be expensive PHP is great for database programming. It has support for most of the DBMSs available today, such as Oracle, MySQL, PostgreSQL, Sybase, and DB2 (see Chapters 17-19 for more details). It has an established strategy to build or use a database abstraction class that can handle all the regular database operations. You can easily change the database without greatly changing the code written for a particular platform. Building a true data layer for the application will result in no code changes to the application logic and the presentation layer (increasing code maintainability). However, abstraction is the best way to go to be completely scalable in terms of data storage. Import ant It is best to consider PEAR (PHP Extension and Add-on Repository) or PHPLib to find a database abstraction class for your DBMS. PEAR is an effort from PHP developers to build a common repository for reusable pieces of code, similar to Perl’s CPAN. You can obtain more information about what is PEAR and how to write or use PEAR code from http://pear.php.net/. However, we will look at writing our own custom abstraction class in Chapter 17. It’s not advisable to use native PHP functions for direct database connectivity with applications. XML Model XML (Extended Markup Language), a recognized standard from the W3C (World Wide Web Consortium), is an excellent data modeling language. XML stored data is modeled by a set of DTDs or schemas that define the structure of XML documents in an industry or task-based way. Today there are hundreds of applications and systems built using XML, both for interchanging and storing data. Since XML is a great standard for converting data from one format to another, it is fast becoming a cornerstone for application interaction. Even if applications do not have to interact with other systems, the use of XML can standardize internal structures, simplifying the development process. Let’s consider this single XML file for the polls application:
Which is your favorite color?

Votes: 4 bytes. Comments file: Binary file, variable

Monday, January 29th, 2007

Votes: 4 bytes. Comments file: Binary file, variable length registers. Structure: name length (1 byte) + name + comment length (2 bytes) + comment Current poll: Binary file, fixed length registers. Pollname: 40 bytes. If we want to find which poll is the current poll, we can open the Current pollfile, read 40 bytes, and get the pollname. We can then get the question for the poll by sequentially searching the pollnamein the Pollfile. The same sequential search can be adopted to retrieve all the options. Voting only implies adding one to a selected option. Comments can be added by appending data to the comments file. However, deleting data is a difficult task. First, the data has to be deleted by marking a poll or an option with blanks in its pollname, and then building a packing process that will recreate the file by physically eliminating the logically deleted records. For more on advanced file structures see File Structures and Object Oriented Approaches in C++ from Addison-Wesley (ISBN 0-201874-01-6). A plain files based data model is really useful when dealing with large amounts of data or when very specific searches must be done over a big volume of information and the queries can’t really be adapted to SQL or XML related common queries. For example, search engines such as Google ( http://www.google.com/) use a data model based on plain files to maintain the repository of information that is collected from the Web. The extra time needed to design, code, and maintain the programs that manage this information is compensated with superb performance that is really specific to the application. Using a plain file based data model in PHP is easy since PHP has plenty of file manipulation functions such as flock(), fwrite(), fgets(), fputs(), fopen(), fclose(), fseek(), ftell(), unlink(), file_exists(), filesize(), and so on. These have been explained in Chapter 9. Relational Database Model This is the most common approach for web applications today. SQL statements are used to insert, delete, and update the data. The model is defined by an entity relationship diagram (ERD), where you indicate the entities used and the relationship between them. Then you can convert the ERD into a table structure and use this structure to establish table relationships. Let’s see how we can model our poll application using a relational database: Polls table: pollid integer(4) pollname varchar(40) question varchar(200) Options table: pollid integer(4) option varchar(80) votes integer(4) Comments table: pollid integer(4) comment text Page 530
Note: If you are looking for good and quality webspace to host and run your java application check professional java hosting services

Multi-Tier Multi-tier development can be defined as a

Monday, January 29th, 2007

Multi-Tier Multi-tier development can be defined as a development process where applications are built by components from different layers. Each layer provides services to the other layers, meaning each layer can abstract a particular aspect of the application. This abstraction leads to very maintainable applications since changes to one particular layer can be done without modifying the others. PHP has a lot of features and capabilities that allow the implementation of a multi-tier oriented methodology to programming. The common layers that can be identified with a web application are: . Content layer . Logic layer . Presentation layer Each layer encapsulates a specific part of the application: The Content Layer The content layer consists of components that provide access routes to the application’s data. All the program components built on this layer must go through it to access the application’s data. The most important entity in this layer is the data model. The data model defines how you store your data and how you should manipulate it. It is best to select a data modeling strategy for your content before you actually start adding to the content. The most common data modeling alternatives are: . Plain files model . Relational database model . XML model . Hybrid models These types should be abstracted to the high-level program code using common objects. These are called Data Access Components (or Data Access Objects, simply DAOs). Plain Files Model The idea behind this model is quite simple: you can use binary or text files to store data. The model consists of textual descriptions of the data structure and names to be used for the data files. There are some applications where this type of a data model would be highly useful. For example, massive search engines using indices or hashing with plain files run much faster and have easier maintenance than gigantic data stores. This model can use a very wide range of file structures, from plain sequential files to b-trees, b* trees, b+ trees, hash tables, binomial heaps, union-find structures, and many others. Let’s consider building a web-based poll application. Here we have different polls with a number of options each, we have votes and might even have comments about polls. We could design a data model based on plain files like this: Polls file: Binary file, fixed length registers. Pollname: 40 bytes. Question: 250 bytes. Options file: Binary file, fixed length registers. Pollname: 40 bytes. Option: 80 bytes. Page 529
Note: If you are looking for good and quality webspace to host and run your java application check professional java hosting services

entity. They built administrative tools to create, delete,

Monday, January 29th, 2007

entity. They built administrative tools to create, delete, and modify content in their databases. Content management systems (CMS) promised that editors, journalists, and managers of information would share the creative burden of an active site. Even HTML had to tidy up its act. Browser vendors attempted to transform HTML into a WYSIWYG (What You See Is What You Get) language, adding awkward presentation features beyond its natural capacity. The result was a hybrid of structured content with a lot of presentation tags, which was cluttered with colors, borders, and some other features. The challenge is to master logic, content, and presentation as one architectural bundle. Page 528
Quick Hint: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services