Namespaces
The biggest change in PHP 5.3 is undoubtedly Namespaces (there was a related PHP Namespaces FAQ before). This brings many benefits to PHP developers, and the widely criticized function naming problem has also been solved.
Code is clearer
Common code before 5.3 requires a custom prefix to distinguish function and class namesPLAIN
TEXT
CODE:
function MY_wrapper() {}
class MY_DB { }
define('MY_CONN_STR', '');
MY_wrapper();
new MY_DB();
MY_CONN_STR;
After using namespaces, the code looks cleaner.
PLAIN TEXT
CODE:
namespace MY;
function wrapper() {}
class DB { }
const CONN_STR = '';
use MY AS MY;
wrapper();
new DB();
CONN_STR;
Multiple namespaces defined in one file
What should be done if multiple namespaces are defined in a file?
PLAIN TEXT
CODE:
namespace LIB;
class MySQL {}
class SQLite {}
$b = new SQLite();
namespace LIB_EXTRA;
class MScrypt {}
$a = new MScrypt();
var_dump(
get_class($a),
get_class($b)
);
The output of the above code is:
PLAIN TEXT
CODE:
string(18)"LIB_EXTRA::MScrypt"
string(11)"LIB::SQLite"
PHP is a language for interpretation and execution, and the above results are reasonable.
namespace priority
Functions, classes and constants defined in the namespace take precedence, followed by global ones.
PLAIN TEXT
CODE:
namespace foo;
function strlen($foo) { return htmlentities($foo); }
echo strlen("test"); // test
echo ::strlen("test"); // 4
echo namespace::strlen("test"); // test
The friendship between namespace and autoload
autoload will parse the class file location based on the namespace name and class name. Autoload will be triggered only when the class definition is not found in the namespace and global scope. __autoload defined in the namespace will not be automatically called.
PLAIN TEXT
CODE:
function __autoload($var) { var_dump($var); } // LIB::foo
require "./ns.php"; /*
<?php
namespace LIB;
new foo();
*/
namespace some accessories
PLAIN TEXT
CODE:
namespace really::long::pointlessly::verbose::ns;
__NAMESPACE__; // New magic constant representing the current namespace name
class a{}
get_class(new a()); // really::long::pointlessly::verbose::ns::a
use really::long::pointlessly::verbose::ns::a AS b;// Reference a class from the namespace Note: The content here is excerpted from pdfIntroduction to PHP 5.3 Slides and will not be repeated later.
Performance improvements
The overall performance of php 5.3 has improved by 5-15%
and md5() is 10-15% faster
Better stack implementation in the engine
Constants moved to read-only memory
Exception handling process improvement (simplification, fewer opcodes)
(require/include)_once improvement, remove duplicate open
Smaller binary size & startup size with gcc4
New language feature__DIR__
Before 5.3, in order to obtain the directory of the current script, a function call was required
: PLAIN TEXT
CODE:
echo dirname(__FILE__); // < PHP 5.3
In 5.3, only one magic constant __DIR__ is needed.
PLAIN TEXT
CODE:
echo __DIR__; // >= PHP 5.3
?:Operator The convenient ?:operator can quickly obtain a non-null value from two values/expressions.
PLAIN TEXT
CODE:
$a = true ?: false; // true
$a = false ?: true; // true
$a = "" ?: 1; // 1
$a = 0 ?: 2; // 2
$a = array() ?: array(1); // array(1);
$a = strlen("") ?: strlen("a"); // 1
__callStatic()
A new magic method __callStatic is added. Its function is similar to __call, but it is only valid for static methods.
PLAIN TEXT
CODE:
class helper {
static function __callStatic($name, $args) {
echo $name.'('.implode(',', $args).')';
}
}
helper::test("foo","bar"); // test(foo,bar)
Dynamically call a static method. Dynamically call a static method? Combination of movement and stillness.
PLAIN TEXT
CODE:
class helper {
static function foo() { echo __METHOD__; }
}
$a = "helper";
$b = "foo";
$a::$b(); // helper::foo
Late Static Binding
I don’t know how to translate it, maybe it’s easier to understand if I leave the original text. The event processing timing of static methods has changed. It used to be processed during compilation, but now it is processed during execution.
Before PHP 5.3, the following code would output an A, but this is not what we want. The whoami method has been redefined in class B. It should output B to conform to what we take for granted.
PLAIN TEXT
CODE:
class A {
public static function whoami() {
echo __CLASS__;
}
public static function identity() {
self::whoami();
}
}
class B extends A {
public static function whoami() {
echo __CLASS__;
}
}
B::identity(); // A <-- PHP <5.3
The following code uses static::whoami() to call static methods. After PHP 5.3, since __CLASS__ is processed during execution, class B can be successfully caught in this example.
PLAIN TEXT
CODE:
class A {
public static function whoami() {
echo __CLASS__;
}
public static function identity() {
static::whoami();
}
}
class B extends A {
public static function whoami() {
echo __CLASS__;
}
}
B::identity(); // B <-->= PHP 5.3
mysqlnd
See that mysqlnd has become the default mysql driver in PHP 5.3
, but PDO_MySQL does not yet support mysqlnd. Currently, only the mysql(i) extension can use
the new features of PHP 5.3 introduced before, which are all convenient for developers. Here is a feature that web hosting providers like very much.
Enhanced ini file support
CGI/FastCGI supports INI configuration similar to .htaccess. Each directory can have INI settings. The file name of ini depends on the configuration of php.ini, but [PATH=/var/www/domain.com], [HOST=www .domain.com] section settings cannot be modified by the user.
Enhanced error handling
Variables and constants are allowed to be defined in ini files and can be called directly in the program.
Attached is an example of an ini file
PLAIN TEXT
CODE:
#User-defined php.ini file name (.htaccess). The default is ".user.ini"
user_ini.filename=".user.ini"
#If you want to disable this feature, set it to a null value.
user_ini.filename=
#User-defined php.ini file TTL length (time-to-live), unit is seconds, I understand it as cache expiration time. Default is 300 seconds
user_ini.cache_ttl=300
[PATH=/var/www/domain.com]
variables_order = GPC
safe_mode =1
[my variables]
somevar = "1234"
anothervar = ${somevar}; anothervar == somevar
[ini arrays]
foo[bar]=1
foo[123]=2
foo[]=3