Press "Enter" to skip to content

Callable Type Hints

PHP 5.4's Zend Engine adds the callable type hint so a function can require a callable argument without manual validation — foo(callable $callback). Works with strings, closures, and [class, method] arrays; otherwise throws a catchable fatal. For extension dev, zend_parse_parameters gains the 'f' flag.

Filed in English, PHP应用
with 0 Comment

Array Dereferencing

PHP 5.4 adds array dereferencing (Felipe's change): you can now directly index into a returned array — $mid = foo()[1] — instead of the old list(, $mid, ) = foo(); or a temp variable. It also works with references, e.g. getTable()["foo"] = "laruence" when the function returns by reference.

Filed in English, PHP应用, 随笔
with 0 Comment

Introducing the mysqlnd_ms Plugin

mysqlnd_ms, a mysqlnd plugin (from the MySQL team: Andrey, Ulf Wendel, johannes), does read-write splitting and load balancing by inspecting queries — reads go to slaves, writes to the master. Configure master/slave in the plugin's ini (JSON from 1.1.0) and connect via a section name like mysqli("myapp", ...). Requires mysqlnd, the default driver since PHP 5.4 beta1.

Filed in English, MySQL/PostgreSQL, PHP应用, 随笔
with 0 Comment

Recursive Patterns in PHP Regex

Can a regex match balanced brackets? Perl 5.6+ introduced recursive patterns: the (?R) symbol refers to the pattern itself, so #\((?R)*\)# matches arbitrarily nested balanced parens — but you must always give the recursion a terminating condition (e.g. (?R)* not (?R), else it tries to match infinitely). Also supports numbered references like (?3).

Filed in English, PHP应用
with 0 Comment

Answering a Question on bugs.php.net (Why __get Is Called Twice After unsetting a Private Property)

Why does echo $example->p1 call __get twice after unset($example->p1)? Laruence walks through zend_read_property: an unset private property isn't found in properties, so __get is invoked; inside __get, $this->$elmname is a read in the class scope where the property IS declared (named Examplep1), triggering a second, differently-named guard before the in_get recursion guard finally stops it.

Filed in English, PHP应用, PHP源码分析
with 0 Comment

Some Resources for PHP Extension Development

Laruence's starter guide for anyone picking up PHP extension work: read 'Extending and Embedding PHP', mine the PHP source's ext/ directory for real Zend_API examples, use the pecl-dev mailing list and efnet #php.pecl for questions, write phpt test cases, build PHP with --enable-debug so your extension runs clean, and check for leaks with valgrind.

Filed in English, 随笔
with 0 Comment

Optimizations in the Zend Engine (PHP 5.4)

How PHP 5.4's 'improved Zend engine performance, reduced memory' actually works: (1) class property/static/const Hashtables are now allocated lazily only when non-empty; (2) opcode operands no longer each carry a zval but point into a per-op-array literal table, shrinking an opcode from 72 to 28 bytes on 32-bit; (3) literal strings become immutable 'internal strings' with pre-computed hashes. Includes dev-team benchmarks (19–26% on micro benches).

Filed in English, PHP源码分析, 随笔
with 0 Comment