Intranet Design Magazine: What's New in PHP4?Aug 27, 2000, 19:44 (1 Talkback[s])
(Other stories by Christophe Lauer)
By Christophe Lauer, Intranet Design Magazine
The long-awaited PHP4 version has been available since the end of May 2000. After having acquired successively Beta-test and Release Candidate status, the final version which can be used for real-world site production is today a reality.
The questions that come up systematically with each new version of a software program are, 'What's new in this version?' and 'Which of these new features are truly useful?' These are certainly legitimate questions, as the simple availability of a new version does not justify migration to it. The thing is that behind what seems to be a simple update looms a myriad of potential problems that must be zeroed in on before moving forward, especially because once updated, there's often no turning back.
I'd like to take a minute to point out that if you are currently questioning the availability date of PHP4 support by your favorite host, it is important to know that such a task represents a considerable amount of work.
In this article, we'll take a quick look at PHP4's new features, which will allow you to appreciate the tremendous work that has been carried out by Zeev Suraski and Andi Gutmans, as well as all of the other contributors.
PHP4 is a complete rewrite of PHP3 at the heart of the language's engine. But why did the language's authors decide to completely rewrite it?
The manner in which PHP3's scripting engine works is not adapted to the execution of rich, large-scale applications such as Phorum or KeyStone. Indeed, this engine reads PHP script instructions line-by-line and then executes them one-by-one, which enables PHP3 to obtain good performance with short and simple scripts, but yields rather mediocre performance with larger applications. It is important to understand that PHP3 was not designed to execute such applications.
It is for this reason that PHP3's two creators decided to implement a scripting engine that would be capable of reading all PHP code and "compiling" it before executing it: Zend. This was not an easy task. Although incompatibilities could be conceived at the time of migration from PHP2 to PHP3, such differences had to be limited as much as possible between PHP3 and PHP4 due to the installed base and the number of PHP3 scripts already in use. We can congratulate PHP4's authors on a job well done because the incompatibilities that do exist are only minor.
Although Zend is an integral part of PHP4, it is in fact a scripting/runtime engine that is independent of the PHP language. On this account, it is possible to use Zend in MySQL as the basis for the development of a stored procedure language specific to this database system.
Without going into the details of internal implementation, we can point out a few things about the Zend/PHP4 couple.
PHP4's new architecture allowed its authors to add an abstraction layer in relation to the Web server at the heart of its language. Up until now with PHP3, we had the choice between using it as a specific module for the Apache server or in CGI mode for all other HTTP servers. With PHP4 it is now possible to imagine better integration with Web servers other than Apache. In fact, this is the case for Microsoft's HTTP server, IIS. PHP4 now works as an ISAPI filter for Microsoft's IIS server. For this reason, different parts of PHP4 code had to be made compliant with use in a multi-thread environment (PHP4 is "thread-safe").
There are many new features concerning PHP4's internal implementation, but they don't really concern PHP programmers directly. Nonetheless, it is worth noting one of the most important points: memory allocation and reference counting mechanisms, in other words those mechanisms that free up resources which were no longer used, were completely rethought. Performance and memory use are thus improved, especially for objects and tables. The more data the variables contain, the more memory is saved.
What are the major differences between PHP3 and PHP4 from a user's (a PHP4 designer/developer) point of view?
With PHP3, it was necessary to build a session system manually by using cookies, by making an identifier a parameter, or by using PHPLib. PHPLib can still be used with PHP4 because it offers other functionalities besides session management.
In PHP4, a whole group of configuration features and directives in the php.ini file take the burden of session and user context management off of the programmer. PHP4 session variables can be stored in a simple ASCII file or in a relational database. All of these features are now documented in the official PHP manual.
These functionalities make it possible to prepare the contents that are displayed via complex processes requiring several database queries, for example, and the sending of results only when all of the queries have been carried out successfully. In the opposite case, you can empty the output buffer contents and display an error message in its place. The example below shows a typical case of how these functionalities are used:
Evaluate for Identical
In passing, it is worth noting that PHP4 now includes Boolean-type data.
COM support on Windows
Since a few lines of code tend to be more useful than a long description, let's look at the following example. The code below does nothing less than boot Word, create a new document from it, add text to this document, and then save everything on the disk before quitting.
Displaying portions of raw
This support is not native to PHP4; it necessitates the addition and configuration of an extension. In order to have this extension at your disposition on a Linux system, it is necessary to recompile PHP4 by specifying the adequate configuration options. These extensions are available in the form of objects that have been precompiled for the Windows environment. These extensions can be downloaded from the official PHP Web site. You must choose the extension that is adapted to the Java version that you use (JDK 1.1.8, 1.2 or 1.3). In our example, we use a simple JDK 1.1.8.
In the php.ini configuration file, you must add two lines in a new "Java" section as in the following example:
You can then make sure that the Java support is configured correctly by checking the contents of phpinfo(). You should find a section similar to the following:
Then, place the Java classes that you would like to use in one of the directories given in the list of paths specified by the java.class.path configuration directive, for example in C:\Program Files\Apache\htdocs\phpjava.
Imagine that you have a Java class named Point, which represents a point defined by its coordinates and the various required methods (a constructor, a default constructor, traditional access methods to set and get proprieties, and a toString method). The Point class could look something like the following:
Thus, as long as Java support is configured correctly and the bytecode file Point.class is in the appropriate place, we can execute the following PHP code:
Which triggers the following display:
Even more interesting is that a PointBis class can be derived from a Point class by overloading its constructors.
In the same way, we can instantiate and use an object of the PointBis class as in the following example:
Which gives the following results:
But you might also want to transfer objects to methods of other objects. Let's take the example of a class that represents a vector. Our vector is defined by its origin (a point), its standard and its direction. In Java, the Vector class can be defined in the following manner:
In PHP, we can instantiate a vector by using the default constructor via the following code:
Which results in the following display:
But we can also use the Vector class constructor to which we will transfer a PHP object representing a Point object. Consider the example below:
This code triggers the following display:
We can see that the vector was originally created by its default constructor: it is a null vector for which the origin is (0,0) having a null standard and a null angle direction. Then, after calling the setProps() method, we make sure that the vector origin corresponds to the coordinates of our myPoint point (11,65).
This simplistic example is a way of verifying whether Java can be used from PHP, whether they are your own Java codes or existing Java classes. This Java gateway has many applications: using CORBA components via IIOP, distributing processes via IIOP, distributing processes via RMI, coding portions of "sensitive" code of our Java applications so that the source code cannot be unveiled, etc.
Finally, it is very important to point out that PHP4 is not distributed under the GNU GPL license as was PHP3, but under a specific license called the PHP License. In essence, the PHP License is quite similar to the BSD license. It authorizes distribution and modification of PHP within open source or proprietary applications. Moreover, Zend, which is an integral part of PHP4, is distributed under yet another license, the Zend License, which was inspired by Troll Tech's QPL.
This license enables authors to take advantage of all of the contributions and corrections made by the community while reserving the right to make money off of the commercial licenses for the Zend library. However, this does not really concern PHP script programmers and designers.
Version 4 of PHP is more than just a simple update. It represents a new implementation that necessitated conception and a total rewrite on the part of Zeev Suraski, Andi Gutmans and all members of the PHP core team. With PHP4, we now have a free solution that is of high technical quality, which will most certainly result in extremely rich professional applications.
The information presented in this article comes from the official PHP channels: the Zend site and official mailing lists dedicated to PHP3 and PHP4.