PHP 5.3 new version features

Source: Internet
Author: User
Tags mysql tutorial

I. mysql tutorial driver mysqlndFor a long time, the php tutorial is to connect to mysql through the mysql client, and now mysql has officially launched the php version of mysql client, and this mysqlnd effectively reduces memory usage and improves performance. Specific can see: http://dev.mysql.com/downloads/connector/php-mysqlnd/http://forge.mysql.com/wiki/PHP_MYSQLND can see that the use of mysqlnd less from the mysql driver to copy data to php extension this step. Mysqlnd uses copy-on-write, that is, to copy and read references during write. Mysqlnd is already built in the source code of php5.3. during compilation, use -- with-mysql = mysqlnd, -- with-mysqli = mysqlnd, and -- with-pdo-mysql = mysqlnd to install the mysqlnd driver. The advantage of mysqlnd is more convenient to compile php, without libmysql, which is already built in the source code.

  1. It is more convenient to compile php. libmysql is not required and is already built into the source code.
  2. Use php to avoid copyright issues
  3. Use php Memory Management and Support php memory limits (memory_limit)
  4. Only one copy of all data in the memory and two copies of libmysql. For more information, see
  5. Provides performance statistics to help analyze bottlenecks
  6. Mysqli supports persistent connections (persistent connections)
  7. Performance is definitely faster than libmysql
  8. Add cache mechanism on driver layer
After reading so many features, there is a conflict. Can the PDO at the abstract layer of the database tutorial bring out the features of different backend servers? If mysql is used as the database, is mysqli a better choice? I always think that mysqli is just an over-product, and PDO is the future. Ii. Performance Improvement
  1. Md5 () improves performance by about 10%-15%.
  2. Better stack implementation in the engine, not clear
  3. Constants are saved in ROM (Constants moved to read-only memory ).
  4. Improved Exception Handling and reduced opcode
  5. Solved the problem of repeatedly opening include (require) _ once. Previously, I used static variables once and finally solved this problem.
  6. Binary files compiled with gcc4 will be smaller
  7. Overall performance improved by 5%-15%
Many people think that the bottleneck of web is db, so the performance of app applications does not matter. I think it is mainly because app expansion is much easier than db expansion, so it will lead to db bottlenecks, however, this does not mean that you do not have to worry about the performance of the app. After all, you still need to solve various problems in the app. As a programmer, writing high-quality code is the most basic requirement. The memory usage of the program is low and the execution speed is faster, which is very effective in high concurrency. Sometimes you can change the implementation method to dozens of times higher, which is also normal, of course, I don't need to be too persistent if I have to pay a lot of GAINS. I think I must be aware of writing high-quality code. Iii ,? : OperatorIt is actually in js |, the returned result is not a logical type, but the value of the original variable, such as false? : 123 returns 123 instead of true. The syntax is a bit strange!

4. namespace)

This is a good function. Before we add it, we use a prefix to solve naming pollution. The method is a little tricky.
5. Delayed Static Binding)I guess php's static state is fixed during pre-compilation, so during inheritance, self in the parent class refers to the parent class, not the subclass. Php5.3 adds the new syntax static, which can capture the current class at runtime. A typical example is the single-piece mode:
class ParentClass {    
static private $_instance;
private function __construct() {
}
static public function getInstance() {
if (!isset(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
}
If this parent class is inherited, the Child class's single piece must overwrite the getInstance of the parent class. 5.3 support for delayed binding with static, but unfortunately, even if there is a static keyword, new static cannot be used for instantiation, but there is a work und.
Class ParentClass {
Static private $ _ instance;
Private function _ construct (){
}
Static public function getInstance (){
If (! Isset (self ::$ _ instance )){
$ Class = static: getClass (); // use static to Delay Binding
Self: $ _ instance = new $ class ();
}
Return self: $ _ instance;
}
Static public function getClass (){
Return _ CLASS __;
}
}
This new feature may be faulty and is not recommended for the time being. 6. New magic function _ callStaticIt is actually the static version of _ call. If the called static method does not exist, this magic function will be called, but it is inefficient. 7. Variable Static call)Previously, you can call object methods through variables, such as $ instance-> $ method (); but static methods are not supported. After 5.3, you can use $ someClass :: $ method () is called, but inefficient. 8. Date function date_create_from_formatConverts a string to a timestamp. If strtotime is used, the date format is parsed by php, and date_create_from_format of 5.3 can set the date format of the string,
$ Date = strtotime ("08-01-07 00:00:00"); // php considers the format to be year-month-day
Var_dump (date ("Y-m-d", $ date); // string (10) "2008-01-07"
$ Date = date_create_from_format ("m-d-y", "08-01-07"); // tell php that the format is month-day-year
Var_dump ($ date-> format ('Y-m-d'); // string (10) "2007-08-01"
9. Lambda functions and closures)Javascript supports functional programming. The unpleasant thing about php is that functions are too formal, but php is more free after 5.3. Syntax for defining anonymous Functions
$lambda = function () { echo "Hello World!n"; };
function replace_spaces ($text) {   
$replacement = function ($matches) {
return str_replace ($matches[1], ' ', ' ').' ';
};
return preg_replace_callback ('/( +) /', $replacement, $text);
}
function replace_spaces ($text) {   
return preg_replace_callback ('/( +) /',
function ($matches) {
return str_replace ($matches[1], ' ', ' ').' ';
}, $text);
}
Syntax for generating closures
function (normal parameters) use ($var1, $var2, &$refvar) {}
The use syntax is introduced and variables can be referenced. 10. New magic constant _ DIR __In the past, only _ FILE __was used. To obtain the path of the current FILE, dirname (_ FILE _) is used to obtain the result. Now, _ DIR _ can be used instead. 11. NOWDOCThe php-defined string has a format called the delimiter.
$foo = <<this is $fubar 
ONE;
This method is called HEREDOC. php will parse the variables, and sometimes we do not need to parse the variables. 5.3 is added with NOWDOC, which is actually a single quotation mark of the delimiter.
$bar = <<<'TWO' this is $fubar TWO;
In this way, php only treats it as a string and the variable will not be parsed. 12. GCDue to the working mechanism of php itself, the GC mechanism of php is sufficient as long as it is efficient. A small amount of memory leakage is allowed. After all, after the program is executed, all requested memory will be released, there will be no memory leakage at all, but this is only for a short-running program, if you use php to write a persistent execution, you need to consider the memory leakage issue.
The GC mechanism of php uses the reference counting mechanism. The reference counting mechanism is very simple and efficient, but its disadvantage is also obvious. It cannot completely recycle all invalid variables, for example, variables are referenced by each other, the GC function added in 5.3 is actually used to enhance the GC mechanism.
Gc_enable (); // activate GC, enhance GC mechanism, and recycle invalid variable referenced by Loop
Var_dump (gc_collect_cycles (); // forcibly reclaim invalid Variables
Gc_disable (); // disable GC

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.