search

Namespaces
php 5.3最大的改动,毫无疑问就是Namespaces(此前有一篇相关的PHP Namespaces FAQ)。这给php开发人员带来的好处不少,广为人们所诟病的函数命名问题也得到了解决。

代码更清晰
5.3之前常见的代码,需要自定义前缀区分函数和类名

PLAIN TEXT

CODE:

function MY_wrapper() {}
class MY_DB { }
define('MY_CONN_STR', '');
MY_wrapper();
new MY_DB();
MY_CONN_STR;
 

使用名称空间之后,代码看上去更加clean。

PLAIN TEXT

CODE:

namespace MY;
function wrapper() {}
class DB { }
const CONN_STR = '';
use MY AS MY;
wrapper();
new DB();
CONN_STR;
一个文件中定义了多个namespace
如果一个文件中定义了多个namespace,应该怎样处理?

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)
);
以上代码输出为:

PLAIN TEXT

CODE:

string(18)"LIB_EXTRA::MScrypt"
string(11)"LIB::SQLite"
php是解释执行的语言,以上结果合情合理。

namespace的优先级
namespace中定义的函数,类和常量优先,其次才是全局的。

PLAIN TEXT

CODE:

namespace foo;
function strlen($foo) { return htmlentities($foo); }
echo strlen("test"); // test
echo ::strlen("test"); // 4
echo namespace::strlen("test"); // test
namespace和autoload的友情
autoload会根据namespace名称以及class名称来解析类文件位置
仅当namespace和全局范围都没找到class定义的情况下,autoload才会被触发
在namespace中定义的__autoload不会被自动调用
PLAIN TEXT
 
CODE:
function __autoload($var) { var_dump($var); } // LIB::foo
require "./ns.php"; /*
              namespace LIB;
         new foo();
*/
namespace一些辅料
PLAIN TEXT

CODE:

namespace really::long::pointlessly::verbose::ns; 
__NAMESPACE__; // 新增的魔法常量,表示当前namespace名称
class a{} 
get_class(new a()); // really::long::pointlessly::verbose::ns::a
use really::long::pointlessly::verbose::ns::a AS b;// 从名称空间中引用一个类
注:这里的内容节选自pdfIntroduction to PHP 5.3 Slides,后文不再复述。

性能提升
php 5.3的总体性能提升了5 - 15%

md5()快了10-15%
Better stack implementation in the engine
Constants移到read-only内存里
exception处理过程改进(简化,opcodes更少)
(require/include)_once改进,去掉重复open
Smaller binary size & startup size with gcc4
新语言特性__DIR__
在5.3以前,为了获得当前脚本的目录,需要一次函数调用

PLAIN TEXT

CODE:

echo dirname(__FILE__); //  

在5.3,只需要一个魔术常量__DIR__就解决了。

PLAIN TEXT

CODE:

echo __DIR__; // >= PHP 5.3
?:操作符
便捷的?:操作符,可以从两个值/表达式中快速取得非空值。

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()
新增了魔术方法__callStatic,功能和__call类似,但是仅对static方法有效。

PLAIN TEXT

CODE:

class helper {
     static function __callStatic($name, $args) {
         echo $name.'('.implode(',', $args).')';
     }
}
helper::test("foo","bar"); // test(foo,bar)
动态调用static方法
动态的调用静态方法?动静结合。

PLAIN TEXT

CODE:

class helper {
     static function foo() { echo __METHOD__; }
}
$a = "helper";
$b = "foo";
$a::$b(); // helper::foo
Late Static Binding
不知道怎么译,可能留个原文更容易理解。静态方法的事件处理时机有变化,以前是在编译期处理,现在是执行期间处理。

在php 5.3之前,下面的代码会输出一个A,但是这不是咱们要的,whoami方法已经在class B中重新定义,它本该输出B才符合咱们想当然的思维。

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 下面代码中使用了static::whoami()来调用静态方法。php 5.3之后,由于__CLASS__是在执行期被处理,那么这个例子中能顺利抓到class B。

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
见mysqlnd成为php 5.3中的默认mysql驱动

但是PDO_MySQL暂时还不支持mysqlnd,目前只有mysql(i)扩展可以用到

之前介绍的php 5.3的新特性,都是方便开发人员的东东。下面介绍个很讨虚拟主机提供商喜欢的特性。

增强的ini文件支持
CGI/ FastCGI支持类似.htaccess的INI配置
每个目录下都可以有INI设置,ini的文件名取决于php.ini的配置,但是[PATH=/var/www/domain.com], [HOST=www.domain.com]段落的设置用户不能修改。
增强的error handling
允许在ini文件中定义变量和常量,可以在程序中直接调用。
附上一段ini文件的例子

PLAIN TEXT

CODE:

#用户自定义的php.ini文件名(.htaccess). 默认是".user.ini"
user_ini.filename=".user.ini"
 
#如果要禁用这个特性,设置为空值即可
user_ini.filename=
 
#用户自定义的php.ini文件TTL时长(time-to-live),单位为秒,我理解为缓存过期时间。默认为300秒
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

Statement
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
Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

See all articles

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 Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),