The new language features of PHPV5 can significantly improve the maintainability and reliability of code. By reading this article, you will learn how to use these new features to migrate the code developed with PHPV4 to PHPV5.PHPV5 and make significant improvements on the basis of PHPV4. New language features make building reliable
Using the new language features of PHP V5 can significantly improve the maintainability and reliability of 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 major improvements based on PHP V4. New language features make it easier to build reliable class libraries and maintain class libraries. In addition, rewrite the standard library to help make PHP more compliant with the same Web language, such as Java? Programming language. Let's take a look at some new PHP object-oriented features and learn how to migrate existing PHP V4 code to PHP V5.
First, let's first understand the features of the new language and how the PHP creation program has changed the method for creating objects using PHP V4. The idea of using V5 is to create an industrial language for Web application development. That means you need to understand the limits of PHP V4 and then learn from other languages (such as Java, C #, C ++, Ruby, and Perl) extract known and outstanding language architectures and integrate these architectures into PHP.
The first and most important new feature is the access protection for class methods and instance variables-public, protected, and private keywords. This new feature allows the class designer to ensure control over the internal characteristics of the class, and also tells the class users which classes can and which classes cannot be touched.
In PHP V4, all code is public. In PHP V5, class designers can declare which code is externally visible (public) and which code is only visible (private) to the class) or only visible to the subclass of the class (protected ). Without such access control, the development of code in a large team or the distribution of code as a library will be blocked, because users of those classes are likely to use incorrect methods or access code that should be private member variables.
Another major new feature is the keyword interface and abstract, which allow contract programming. Contract programming means that one class provides a contract to another class-in other words: "This is what I want to do, and you don't need to know how it is done ". All classes implementing the interface follow this contract. All users of the interface agree to use only the methods specified in the interface. Abstract keywords make it easy to use interfaces. I will describe them later.
These two main features-access control and contractual programming-allow large coding teams to use large code libraries more smoothly. These features also enable IDE to provide a richer set of intelligent language features. This article not only illustrates several migration issues, but also takes some time to explain how to use these new main language features.
Access control
To demonstrate the features of the new language, I used a class named Configuration. This simple class contains configuration items for Web applications-for example, the path to the image directory. Ideally, this information will be stored in a file or database. Listing 1 shows a simplified version.
Listing 1. access. php4
_items[ 'imgpath' ] = 'images'; } function get( $key ) { return $this->_items[ $key ]; }}$c = new Configuration();echo( $c->get( 'imgpath' )."\n" );?> |
This is a fully Orthodox PHP V4 class. The member variable stores the list of configuration items, constructs the loaded items of the program, and then namedget()
.
After running the script, the following code is displayed in the command line:
Good! This result indicates that the code runs normally and is set and read properly.imgpath
The value of the configuration item.
The first step to convert this class to PHP V5 is to rename the constructor. In PHP V5, the method for initializing an object (constructor) is called__construct
. This small change is as follows.
Listing 2. access1.php5
_items[ 'imgpath' ] = 'images'; } function get( $key ) { return $this->_items[ $key ]; }}$c = new Configuration();echo( $c->get( 'imgpath' )."\n" );?> |
This change is not significant. Just move to the PHP V5 convention. The next step is to add access control to the class to ensure that the class users cannot directly read and write$_items
Member variable. The change is as follows.
Listing 3. access2.php5
_items[ 'imgpath' ] = 'images'; } public function get( $key ) { return $this->_items[ $key ]; }}$c = new Configuration();echo( $c->get( 'imgpath' )."\n" );?> |