New Features of PHP v5.3.0

Source: Internet
Author: User
Tags define local php language
As the popular PHP language continues to evolve, many new features make it more object-oriented. This article uses some PHP v5.3 instances to demonstrate delayed static binding, namespace support, class method overloading, variable parsing, and heredoc support.

Requirement

In addition to having a basic understanding of PHP and HTML, you can read this article without much other knowledge. Whenever PHP v5.3 is mentioned in this Article, it refers to v5.3.0.

Features

This article mainly describes the following features of PHP v5.3:

    • Delayed static binding
    • Namespace
    • Class Method overload
    • Variable parsing and heredoc

However, you need to set PHP v5.3 before continuing.



 

Set

A well-known feature of PHP is that it is difficult to set up. This may be because PHP is installed on a Web server (such as APACHE) and often needs to be connected to an external database (such as MySQL ). Moreover, in a sense, PHP scripts are embedded in HTMLCode. In other words, PHP is a technology that spans multiple complex fields. Therefore, you must overcome many obstacles before writing PHP script code. I hope this situation can be improved, but many things related to software technology are still very difficult.

However, for those lucky readers who use Apple Macs, the setting process is much simpler:

    1. Get the PHP binary Installation File (http://www.entropy.ch/software/macosx/PHP ).
    2. Disable Web Sharing in system preferences.
    3. Find the/etc/apache2/httpd. conf file.
    4. Comment out the following line in the httpd. conf file: loadmodule php5_module.
    5. Run the PHP v5.3 Installation File (you can also buildSource code).
    6. Open Web Sharing in system preferences.

The purpose of this article is not just to introduce how to install it, so if your platform is Microsoft Windows or Linux, please refer to some great PHP reference books (see the books I recommend in references ).

To check whether PHP is installed and the version is appropriate, create a file named phpinfo1.php in the/library/webserver/Documents folder. Listing 1 shows the content of this script file. All the scripts in this article are in the compressed (ZIP) file provided in the download section, so you do not need to create any files.

Listing 1. phpinfo1.php

<? PHP
Phpinfo ();
?>

Put the script file in the/library/webserver/Documents folder and access http: // localhost/phpinfo1.php. You can see a page similar to figure 1. Here, you may need to replace "localhost" in the URL with the IP address of your host ".

Figure 1. php v5.3.0 installed successfully
 

Delayed static binding

PHP v5.3 extends the use of static. Currently, you can access static class methods and attributes without instantiating classes. This feature is convenient when the class does not have an object or does not require an object. Listing 2 shows an example of delayed static binding.

Listing 2. Delayed static binding

<? PHP
Class {
Public static function who (){
Echo 'calling who method from class'. _ class __;
}
Public static function test (){
Static: WHO ();
}
}

Class B extends {
Public static function who (){
Echo 'calling who method from class'. _ class __;
}
}

B: Test ();
?>

The code in Listing 2 generates the following output:

Calling who method from Class B

Enhance object-oriented

Any change to PhP's object-oriented enhancement is encouraging, as this will lead to a more reliable design and easier-to-maintain code. For the code in Listing 2, I have some microterms. The WHO () method in the base class (A) and the derived class (B) is repeated. If Class B does not need to copy the code, it seems that the _ class _ syntax in PHP v5.3.0 must do so. That is to say, __class _ does not support delayed binding.

Using _ class _ in Listing 2 may be a bit stubborn, but it does indicate a small problem in PHP v5.3.0. Obviously, this is not a big problem for the example in Listing 2, but it is not that easy for websites with thousands of lines of PHP code. Repeated Code usually means repeated work.

Before PHP v5.3.0, methods in Class A may be defined as self: WHO (). Unfortunately, this will lead to calling the function in Class A, rather than the function in subclass B. PHP v5.3.0 uses static: ** to fix this problem. You can reference the previously called class methods only during runtime. Obviously, this is the purpose of the code in Listing 2.


Namespace

PHP v5.3 provides the namespace function to enhance the encapsulation capability of the language. Namespaces are very common in modern languages, such as C # and XML. The main advantage of a namespace is that it can be used to define independent containers for code symbols (classes, functions, and constants). This may sound complicated, but it is actually not. Listing 3 shows an example of using a PHP namespace to define local and global string constants.

Listing 3. php namespace

<? PHP
Namespace test;
Define ('astring', 'Hello world! ');
Define ('testastring', 'Hello world from my namespace! ');

Echo "<p> my string is :";
Echo astring;
Echo "</P> ";

Echo "<p> my string is ";
Echo astring;
Echo "</P> ";
?>

The code in listing 3 generates the following output:

My string is: Hello world from my namespace!
My string is: Hello world!

So what happened in the code? The first define statement creates a String constant astring in the namespace test. Then, when you reference the astring, refer to the local test namespace first. This is why the locally defined string is first displayed. Then, to see the globally defined string, you only need to use the syntax astring. With these simple rules, you can use any number of namespaces without worrying about name conflicts.

With the namespace function, the boundaries between codes are clearer than before. This helps code integration in the team environment. In other words, different teams can allocate their own names to empty spaces. When all team members follow this Convention, name conflicts should be reduced to zero. This is also an enhancement of PHP language object-oriented in version 5.3.0.

In PHP, term overload is slightly different from the common object-oriented context. In Java or C #, the "overload" method provides the same code for different parameter lists. For example, if there is a method named draw (string Str), the reload method can take the following form: Draw (string STR, int I ). The method symbol name is called a method signature. In order to facilitate overloading, the return type is omitted.

In PHP, "overload" refers to the dynamically created methods and attributes. This is actually a dynamic code. Any such dynamic entity is handled by the so-called magic methods. You can create these magic methods for different actions in a class. Then, when the Code encounters an attribute or method that is not declared or invisible, you can call the overload method. This style overload provides a great deal of flexibility.

It sounds a little complicated, so let's take a look at a specific example. Listing 4 shows how to use PHP to overload call objects and class methods.

Listing 4. php Overloading

<? PHP
Class overloadedmethodtest {
Public Function _ call ($ name, $ arguments ){
// The value of $ name is case sensitive.
Echo "calling object method '$ name '"
. Implode (',', $ arguments ). "N ";
}

/** As of PHP 5.3.0 */
Public static function _ callstatic ($ name, $ arguments ){
// The value of $ name is case sensitive.
Echo "calling static method '$ name '"
. Implode (',', $ arguments ). "N ";
}
}

$ OBJ = new overloadedmethodtest;
$ Obj-> runoverloadedtest ('in an object context ');

Overloadedmethodtest: runoverloadedtest ('in a static context'); // as of PHP 5.3.0
?>

The code in Listing 4 generates the following output:

Calling object method 'runoverloadedtest' in an object context
Calling static method 'runoverloadedtest' in a static Context

In Listing 4, note how the overloaded code infers method names and Parameters Based on the called code:

$ Obj-> runoverloadedtest ('in an object context ');

Overloadedmethodtest: runoverloadedtest ('in a static context'); // as of PHP 5.3.0

To use this capability,ProgramPersonnel must take greater responsibilities. While overloading facilitates the compilation of flexible code, it also provides errors. Therefore, be cautious when using heavy loads and have strict code testing discipline.



   

Variable parsing and heredoc

One of the advantages of PHP is string parsing. PHP has no limit on the length of the string, as long as the available memory of the host is large enough. Listing 5 shows the flexibility of using PHP.

Listing 5. php Parsing

<? PHP
$ Beverage = 'coffee ';
// The following works; "'" is an invalid
Character for variable names
Echo "$ beverage's taste is great ";
// The following won't work;'s is a valid
Character for variable names but
Echo "he drank a number of $ beverages ";
Variable is "$ beverage"
Echo "he drank some $ s"; // works
Echo "he drank some s"; // works
?>

The code in listing 5 generates the following output:

Coffee's taste is great
He drank a number
He drank some coffees
He drank some coffees

The code in listing 5 is not available until PHP v5.3.0. This code is provided here to remind you of the string processing support and some parsing rules. However, PHP v5.3.0 adds enhanced heredoc syntax support. Listing 6 shows a simple example.

Listing 6. php v5.3.0 heredoc Application

<? PHP
Echo >>> "foobar"
Hello world!
Foobar;
?>

The code in Listing 6 generates the following amazing output:

Hello world!

So what's special about listing 6? Heredoc in PHP v5.3.0 supports double quotation marks. The main advantage is that the content in double quotation marks is not parsed, so that more flexibility is available. Basically, this only adds more flexibility on the basis of strong string support. The online reference of PHP v5.3.0 includes more information about this topic and other related topics of PHP v5.3.0.



 

Conclusion

PHP is an attractive technology. It facilitates pick-and-mix website development. You can add a database for the website. The PHP language provides the required binding for this purpose. You do not need heavyweight application servers or servlet technologies. In other words, you only need a very small investment-financial or intellectual investment-to control the environment.

The PHP language is constantly evolving. We are glad to see many improvements in v5.3.0 have effectively enhanced the object-oriented aspect. This helps PHP win in the competition for more important (and more expensive) web design methods.

In this article, I have discussed latency static binding, namespace support, class method overloading, and heredoc support. This is just the tip of the iceberg of PHP v5.3.0. There are many other topics, such as migrating to v5.3.0. For more information about these topics and other topics, see the references section.

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.