Linux Today: Linux News On Internet Time.
Search Linux Today
Linux News Sections:  Developer -  High Performance -  Infrastructure -  IT Management -  Security -  Storage -
Linux Today Navigation
LT Home
Contribute
Contribute
Link to Us
Linux Jobs


Top White Papers

More on LinuxToday


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.

  • What's the difference between PHP3 and PHP4?

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.

  • A Developer's View

What are the major differences between PHP3 and PHP4 from a user's (a PHP4 designer/developer) point of view?

Sessions
One of PHP4's most anticipated functions is native session support. Indeed, nothing in the language's base functions provided for management of user sessions, in other words all of the values and variables that are linked to a user and can be transferred from page to page within the same application.

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.

Output Buffering
With PHP3 all of the content produced by echo () or print() was directly sent to the server. In PHP4 an intermediate storage layer makes it possible to defer the sending the Web server's output flow. In order to do this, new features, which haven't been documented yet, are available:

  • ob_start(): activates output buffering
  • ob_end_flush(): sends the contents of the output buffer and deactivates buffering
  • ob_end_clean(): empties the output buffer and deactivates buffering
  • ob_get_contents(): returns the output buffer contents

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:

<?php
// activate output buffers
ob_start();
// the output is not displayed, it enters the buffer print "Hello, world!";
// read buffer contents
$output = ob_get_contents();
// deactivate the buffer, empty the buffer
ob_end_clean();
// display contents
print $output;
?>

Evaluate for Identical Operator
A new evaluate for identical operator makes its debut in PHP4. This operator makes it possible to test the equality of the values and types of two different variables or expressions. With implicit PHP-type conversions,the value 5 could be compared with the string containing the character "5." These two values were considered equal in a comparison. The new operator is represented by the three equal signs ("= = =") and only sends back TRUE if the variable type and value are identical. Thus, in the following example, the four tests using the equality operator with the numbers 1 to 4 are true, while with the following four, which use the evaluate for identical operator, only tests 5 and 8 are true.

$valint1 = 1;
$valint2 = 2;
$valint3 = 3;


$valfloat1 = 1.0;
$valfloat2 = 2.0;
$valfloat3 = 3.0;

$valstr3 = '3';

$res = (($valint1+$valint2)==$valint3) ? ('TRUE'):('FALSE') ;
print " TEST 1 : " . $res . "<br>\n";

$res = ($valint3==$valfloat3) ? ('TRUE') : ('FALSE') ; print " TEST 2 : " . $res . "<br>\n";

$res = ($valstr3==$valint3) ? ('TRUE') : ('FALSE') ; print " TEST 3 : " . $res . "<br>\n";

$res = (($valfloat1+$valfloat2)==$valfloat3) ? ('TRUE') : ('FALSE') ; print " TEST 4 : " . $res . "<br>\n<br>";

"; $res = (($valint1+$valint2)===$valint3) ? ('TRUE') : ('FALSE') ;
print " TEST 5 : " . $res . "<br>\n";

$res = ($valint3===$valfloat3) ? ('TRUE') : ('FALSE') ; print " TEST 6 : " . $res . "<br>\n";

$res = ($valstr3===$valint3) ? ('TRUE') : ('FALSE') ; print " TEST 7 : " . $res . "<br>\n";

$res = (($valfloat1+$valfloat2)===$valfloat3) ? ('TRUE') : ('FALSE') ; print " TEST 8 : " . $res . "<br>\n";

In passing, it is worth noting that PHP4 now includes Boolean-type data.

COM support on Windows
On Windows platforms, PHP4 now offers support for COM components. This means that practically all Windows applications can be manipulated from PHP4. The COM model that is at the heart of Microsoft's architecture is such that any Windows application can provide a certain number of components and exposed methods, which are entry points for manipulating applications from other applications.

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.

<?php
$word = new COM("word.application") or
 die("Impossible to instantiate WordApp");
print "Word is running, version {$word->Version}\n</br>";

$word->Visible=1;

$word->Documents->Add();
$word->Selection->TypeText("This is a test..."); $word->Documents[1]->SaveAs("test_com_php.doc"); $word->Quit();
?>

Displaying portions of raw HTML code
Another newcomer to PHP4 is a new syntax that is modeled after Perl's "here printing." It is possible to display complete portions of HTML code by flanking them with either a print or echo instruction and an end label. Unlike Perl, the operator used is not a double less than sign (<<), but a triple less than sign (<<<) so as to distinguish it from the binary shift operator. For example:

<?php


$version = phpversion();
$valstr = "Example of HerePrinting\n";

echo "<hr><br>/n";

print <<< mylabel

<h4>PHP's New Features</h4>
<ul>
<li>ISAPI support
<li>COM support on Win32
<li>Java and servlets support
</ul>

<p>$valstr <br>Tested on PHP version $version</p>

mylabel;

echo "<hr><br>/n";
?>

Java support
Java support represents without a doubt one of the new features that will open PHP up to existing enterprise systems and applications. With PHP4, you have the possibility of instantiating and using Java servlets and/or classes.

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:

[java]
java.class.path="c:\jdk1.1.8\lib\classes.zip;
C:\Program Files\Apache\cgi-bin\php_java.jar;
C:\Program Files\Apache\htdocs\phpjava" extension=php_java.dll

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:


Java configuration in php.ini

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:

public class Point {

// attributes
public int abscissa;
public int ordinate;

public int type;

// default constructor
public Point() {
  setCoordinates(0,0);
  this.type = 1 ;
}
// constructor
public Point(int x,int y) {
   setCoordinates(x,y);
   this.type = 1 ;
}
// Overloaded object method
public toString String(){
   // Call the display method
   return display();
}
// Point class methods
public void setType(int mytype){
   // Call the display method
   this.type = mytype;
}
public int getAbscissa() { return abscissa; }
public int getOrdinate() { return ordinate; }
public void setCoordinates(int abscissa, int ordinate) {   this.abscissa = abscissa;
  this.ordinate = ordinate;
}
public sayHello String() {
  return "I am a point :-)";
}
public display String(){
return "("+abscissa+","+ordinate+" Type "+type+")";
}
}

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:

<?php

$myPoint = new Java("Point");
echo "<br>Point created at (0,0) ...";

echo $myPoint->setCoordinates(15,15);
echo "<br>Point moved to (15,15)";
echo "<br>".$myPoint->display();
echo "<br>".$myPoint->sayHello();
echo "<hr>\n";


?>

Which triggers the following display:

Point created at (0,0) ...
Point moved to (15,15)
(15,15 Type 1)
I am a Point :-)

Even more interesting is that a PointBis class can be derived from a Point class by overloading its constructors.

public class PointBis extends Point {

// constructors
public PointBis() {
  setCoordinates(0,0);

super.setType(2);
}
public PointBis(int x,int y) {
  setCoordinates(x,y);

  super.setType(2);
}

// methods
public sayHello String() {
  return "I am a PointBis ;^)";
 }
}

In the same way, we can instantiate and use an object of the PointBis class as in the following example:

$myPointBis = new Java("PointBis");
echo "<br> Point created at (0,0) ...";

echo $myPointBis->setCoordinates(17,21);
echo "<br>Point moved to (17,21)";
echo "<br>".$myPointBis->display ();
echo "<br>".$myPointBis->sayHello();

Which gives the following results:

Point created at (0,0) ...
Point moved to (17,21)
(17,21 Type 2)
I am a PointBis ;^)

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:

public class Vector {

// attributes
private Point origin;
private int standard;
private int direction;

// constructors
public Vector() {
  Point p1 = new Point();
  setProps(p1, 0, 0);
}
public Vector(Point orig, int s, int d) {
setProps(orig, s, d);
}

// Overloaded Object method
public toString() String{
  // Call the display method
  return display ();
}
// Vector class methods
public Point getOrigin () { return origin; }
public int getStandard() { return standard; }
public int getDirection() { return direction; }
public void setOrigin (Point origin) {
  this.origin = origin;
} ;
public void setStandard(int standard) {
  this.standard =standard;
} ;
public void setDirection(int direction) {
  this.direction = direction;
} ;
public void setProps(Point origin, int standard, int direction) {
  this.origin = origin;
  this.standard = standard;
  this.direction = direction;
}
public display() String {
  return "( "+origin.display()+
      " / "+standard+" / "+direction+" )";
  }
}

In PHP, we can instantiate a vector by using the default constructor via the following code:

$myVector = new Java("Vector");
echo "<br>Vector created, default constructor...";
echo "<br>".$myVector->display()." <p>\n<br>\n";

 }
}

Which results in the following display:

Vector created, default constructor...
( (0,0 Type 1) / 0 / 0 )

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:

// creation of a point that will serve as vector origin $myPoint = new Java("Point", 11, 65);
echo "<br>Point position...";
echo "<br>".$myPoint->display().<p>\n<br>\n";

$myVector = new Java("Vector");
echo "<br>".$myVector->display()."<p>\n<br>\n";

print $myVector->setProps($myPoint, 24, 8);
echo "<br>myVector's origin is MyPoint";

echo "<br>Vector Position...";
echo "<br>".$myVector->display()."<p>\n<br>\n";

This code triggers the following display:

Point Position...
(11,65 Type 1)
( (0,0 Type 1) / 0 / 0 )

myVector's origin is MyPoint
Vector Position...
( (11,65 Type 1) / 24 / 8 )

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.

  • License

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.

  • Final Note

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.

  • Credits

The information presented in this article comes from the official PHP channels: the Zend site and official mailing lists dedicated to PHP3 and PHP4.

Related Stories: