Home Backend Development PHP Tutorial Detailed analysis of the new version of PHP 5.3_PHP Tutorial

Detailed analysis of the new version of PHP 5.3_PHP Tutorial

Jul 13, 2016 pm 05:08 PM
mysql php one analyze Tutorial new version characteristic Detailed explanation pass drive

1. mysql tutorial driver mysqlnd For a long time, PHP tutorials have been connecting to MySQL through the MySQL client. Now MySQL has officially launched the PHP version of the MySQL client, and this MySQLnd can effectively reduce memory usage and improve performance. For details, see: http://dev.mysql.com/downloads/connector/php-mysqlnd/ http://forge.mysql.com/wiki/PHP_MYSQLND As can be seen from the figure, using mysqlnd eliminates the step of copying data from the mysql driver to the php extension. Mysqlnd uses copy-on-write, which means copy-on-write and read-reference. mysqlnd has been built into the source code of php5.3. When compiling, use --with-mysql=mysqlnd, --with-mysqli=mysqlnd and --with-pdo-mysql=mysqlnd to install the mysqlnd driver. Advantages of mysqlnd Compiling PHP is more convenient, no need for libmysql, it is already built into the source code

  1. Compiling PHP is more convenient, no need for libmysql, it is already built into the source code
  2. Use php license to avoid copyright issues
  3. Use PHP memory management and support PHP memory limit (memory_limit)
  4. There is only one copy of all data in the memory. The previous libmysql had two copies. Refer to the picture above
  5. Provides performance statistics function to help analyze bottlenecks
  6. mysqli supports persistent connections
  7. The performance is definitely faster than libmysql
  8. Add caching mechanism in driver layer
After seeing so many features, I feel a bit contradictory. Can PDO, as the abstraction layer of the database tutorial, bring out the features of different backends? If you use mysql as the database, is mysqli a better choice? I always feel that mysqli is just an excessive product, and PDO is the future. 2. Performance improvement
  1. md5() probably improves performance by 10%-15%
  2. Better stack implementation in the engine, I don’t understand
  3. Constants are stored in ROM (Constants moved to read-only memory), I don’t understand the meaning here
  4. Improve exception handling and make the opcode more concise
  5. Solved the problem of repeatedly opening include(require)_once. Before, I used static variables to implement once, and finally solved this problem
  6. Binaries compiled with gcc4 will be smaller
  7. Overall performance improved by 5%-15%
Many people think that the bottleneck of the web is the db, so they don't care about the performance of the app. I think it is mainly because app expansion is much easier than db expansion, so the db bottleneck occurs, but this does not mean that we can ignore the app's performance. Performance, after all, will ultimately solve various problems in the app. As a programmer, writing high-quality code is the most basic requirement. Programs that use less memory and execute faster are very effective under high concurrency. Sometimes if you change the implementation method, it is normal to increase it dozens of times. Of course, if you have to pay a lot but get little, Don't be too persistent. I think you must have the awareness to write high-quality code. 3. ?:Operator In fact, it is || in js. The returned result is not a logical type, but the value of the original variable. For example, false ?: 123 returns 123, not true. The syntax is a bit weird!

4. Namespace

This is a very good function. Before it was added, prefixes were used to solve naming pollution. The method was a bit copycat, haha.
5. Late Static Binding I estimate that the static nature of PHP is fixed during pre-compilation, so when inheriting, self in the parent class refers to the parent class, not the subclass. PHP5.3 adds a new syntax, static, which can capture the current class at runtime. A typical example is the singleton mode:
class ParentClass {    <br>static private $_instance;   <br>private function __construct() {   <br>}   <br>static public function getInstance() {   <br>if (!isset(self::$_instance)) {   <br>self::$_instance = new self();   <br>}   <br>return self::$_instance;   <br>}   <br>}
Copy after login
If you inherit this parent class, the singleton of the subclass must override the getInstance of the parent class. 5.3 supports using static to delay binding, but unfortunately, even if there is a static keyword, new static cannot be used to instantiate it, but there is a workaround.
class ParentClass {    <br>static private $_instance;   <br>private function __construct() {   <br>}   <br>static public function getInstance() {   <br>if (!isset(self::$_instance)) {   <br>$class = static::getClass();// 使用static延迟绑定   <br>self::$_instance = new $class();   <br>}   <br>return self::$_instance;   <br>}   <br>static public function getClass() {   <br>return __CLASS__;   <br>}   <br>}
Copy after login
这个新特性当前可能会有问题存在,暂时不建议使用。 六、新的魔法函数__callStatic 其实就是__call的静态版,调用的静态方法不存在会调用这个魔法函数,但是低效 七、通过变量调用静态(Variable Static Calls) 以前可以通过变量调用对象的方法,例如$instance->$method();,而静态方法不支持,5.3后就支持了可以通过$someClass::$method()调用了,但是低效。 八、日期函数date_create_from_format 把字符串转换成时间戳,如果使用strtotime的话,日期格式是php解析的,而5.3的date_create_from_format可以设置字符串的日期格式,
$date = strtotime("08-01-07 00:00:00");//php 认为格式 是年-月-日   <br>var_dump(date("Y-m-d", $date)); // string(10) "2008-01-07"  <br>$date = date_create_from_format("m-d-y", "08-01-07");//告诉php格式是 月-日-年 <br>var_dump($date->format('Y-m-d')); // string(10) "2007-08-01"
Copy after login
九、匿名函数(Lambda functions)和闭包(closures) js有个很爽的地方就是支持函数式编程,php很不爽的地方就是函数太过正规了,不过5.3后php更自由了。 定义匿名函数的语法
$lambda = function () { echo "Hello World!n"; };
Copy after login
function replace_spaces ($text) {   <br>$replacement = function ($matches) {   <br>return str_replace ($matches[1], ' ', ' ').' ';   <br>};   <br>return preg_replace_callback ('/( +) /', $replacement, $text);   <br>}
Copy after login
function replace_spaces ($text) {   <br>return preg_replace_callback ('/( +) /',   <br>function ($matches) {   <br>return str_replace ($matches[1], ' ', ' ').' ';   <br>}, $text);   <br>}
Copy after login
产生闭包的语法
function (normal parameters) use ($var1, $var2, &$refvar) {}
Copy after login
引入了use语法,并且支持引用变量 十、新魔法常量__DIR__ 以前只有__FILE__,为了得到当前文件所在的路径,都是用dirname(__FILE__)来得到的,现在可以用__DIR__代替了 十一、NOWDOC php定义字符串有一种格式叫定界符.
$foo = <<this is $fubar <br>ONE;
Copy after login
这种方式称为HEREDOC,php会解析里面的变量,而有时候我们不需要解析变量,5.3加入了NOWDOC,其实就是定界符的单引号版
$bar = <<<'TWO' this is $fubar TWO;
Copy after login
这样,php只把它当作字符串,变量不会解析 十二、GC 因为php本身工作机制的原因,所以php的GC机制只要高效就够了,允许少量内存泄漏,毕竟程序执行完毕就会释放所有申请的内存,根本不会有内存泄漏的问题,但这只是对于短暂运行的程序而言,如果用php编写持久执行就要考虑内存泄漏问题了。
php的GC机制采用引用计数机制,引用计数是很简单高效的GC机制,不过缺点也很明显,不能完全回收所有已无效的变量,例如变量相互引用了,就无法回收了,5.3里加入的GC函数,其实就是起到增强GC机制的作用。
gc_enable(); // 激活GC,增强GC机制,回收循环引用的无效变量 <br>var_dump(gc_collect_cycles()); // 强制回收已无效的变量 <br>gc_disable(); // 禁用GC
Copy after login

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/629847.htmlTechArticle一、mysql教程驱动mysqlnd 一直以来,php教程都是通过mysql客户端连接mysql,而现在mysql官方已经推出php版的mysql客户端,而这个mysqlnd有效降低内...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

An efficient way to batch insert data in MySQL An efficient way to batch insert data in MySQL Apr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Detailed explanation of the installation steps of MySQL on macOS system Detailed explanation of the installation steps of MySQL on macOS system Apr 29, 2025 pm 03:36 PM

Installing MySQL on macOS can be achieved through the following steps: 1. Install Homebrew, using the command /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)". 2. Update Homebrew and use brewupdate. 3. Install MySQL and use brewinstallmysql. 4. Start MySQL service and use brewservicesstartmysql. After installation, you can use mysql-u

See all articles