PHP V5 Migration Guide

Source: Internet
Author: User
Tags abstract command line constructor php and

With the new language features of PHP V5, you can significantly improve the maintainability and reliability of your code. By reading this article, you will learn how to use these new features to migrate code developed with PHP V4 to PHP V5.

PHP V5 has made significant improvements on the basis of PHP V4. The new language features make it easier to build reliable class libraries and maintain class libraries. In addition, rewriting the standard library helps make PHP more compatible with its same Web language, such as the Java™ programming language. Let's take a look at some of the new object-oriented features of PHP and learn how to migrate existing PHP V4 code to PHP V5.

First, learn about the new language features and how PHP's creation program changed the way you create objects with PHP V4. The idea with V5 is to create an industry-level language for WEB application development. That means understanding the limitations of PHP V4 and then extracting known good language architectures from other languages (such as Java, C #, C + +, Ruby, and Perl) and incorporating those schemas into PHP.

The first and most important new feature is the Access Protection--public, protected, and private keywords for class-specific methods and instance variables. This new feature enables class designers to ensure control over the intrinsic attributes of a class, while telling the class's users which classes can and which classes are not accessible.

In the PHP V4, all the code is public. In PHP V5, a class designer can declare which code is visible to the outside (public) and which code is visible only within the class (private) or only to subclasses of the class (protected). Without these access controls, the work of developing code in a large team or distributing code as a library is hampered by the fact that consumers of those classes are likely to use the wrong method or access code that should be private member variables.

Another big new feature is the keyword interface and abstract, which allow for contract programming. Contract programming means that a class provides a contract to another class--in other words, "This is what I do, and you don't need to know how it's done." All classes that implement interface follow the contract. All users of interface agree to use only the methods specified in interface. The abstract keyword makes it easy to use the interface, which I will explain later.

These two main features-access control and contract programming-allow large coders teams to use large code libraries more smoothly. These features also enable the IDE to provide a richer set of language intelligence features. This article not only illustrates several migration issues, but also takes some time to explain how to use these new primary language features.

Access control

To demonstrate the new language features, I used a class called Configuration. This simple class contains configuration items for WEB applications--for example, a path to a picture directory. Ideally, this information will exist in a file or database. Listing 1 shows a simplified version.

Listing 1. Access.php4

<?php
class Configuration
{
 var $_items = array();
 function Configuration() {
  $this->_items[ 'imgpath' ] = 'images';
 }
 function get( $key ) {
  return $this->_items[ $key ];
 }
}
$c = new Configuration();
echo( $c->get( 'imgpath' )."\n" );
?>

This is a completely orthodox PHP V4 class. The member variable holds the list of configuration items, the constructor loads the item, and then the access method named Get () returns the value of the item.

After you run the script, the following code appears on the command line:

% php access.php4
images
%

Very good! This result means that the code is working properly and the value of the Imgpath configuration item is set and read normally.

The first step in converting this class to PHP V5 is to rename the constructor. In PHP V5, the method of initializing an object (constructor) is called __construct. The minor changes are shown below.

Listing 2. Access1.php5

<?php
class Configuration
{
 var $_items = array();
 function __construct() {
  $this->_items[ 'imgpath' ] = 'images';
 }
 function get( $key ) {
  return $this->_items[ $key ];
 }
}
$c = new Configuration();
echo( $c->get( 'imgpath' )."\n" );
?>

This change is not big. Just move to the PHP V5 convention. The next step is to add access control to the class to ensure that the class's consumers cannot read and write directly to the $_ITEMS member variable. The changes are shown below.

Listing 3. Access2.php5

<?php
class Configuration
{
 private $_items = array();
 public function __construct() {
  $this->_items[ 'imgpath' ] = 'images';
 }
 public function get( $key ) {
  return $this->_items[ $key ];
 }
}
$c = new Configuration();
echo( $c->get( 'imgpath' )."\n" );
?>

If the user of this object is to access the item array directly, access is denied because the array is marked private. Fortunately, the user discovers that the Get () method provides a wide range of popular Read permissions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.