making bbPress (and WordPress) work better!

What’s new in PHP 5.3

Besides the 15% speedup for most code (20% in some cases!) and reduced memory use, PHP 5.3 has some very interesting and handy features. Check out this really nice “slideshow” of what’s new in PHP 5.3 by Ilia Alshanetsky:

[PDF] http://ilia.ws/files/phpquebec_php53.pdf

or in plain text (somewhat garbled as formatting is lost:
INTRODUCING PHP 5.3
PHP Quebec 2008 – Ilia Alshanetsky
1

EVOLUTIONARY
RELEASE
Old code will still work
Focus on mainly
improvement of
existing functionality
Fewer bugs
Faster release cycle
2

Namespaces
Biggest addition in 5.3 code base
Feature complete implementation of namespaces
Majority of functionality implemented at compile time
Simplifies naming conventions
3

Cleaner Code
function MY_wrapper() {}
class MY_DB { }
define(‘MY_CONN_STR’, ”);
namespace MY;
function wrapper() {}
class DB { }
const CONN_STR = ”;
MY_wrapper();
new MY_DB();
MY_CONN_STR;
use MY AS MY;
wrapper();
new DB();
CONN_STR;
Without Namespaces With Namespaces
+ =
4

Multiple Namespaces Per File
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)
);
string(18) “LIB_EXTRA::MScrypt”
string(11) “LIB::SQLite”
5

Namespace Hierarchy
namespace foo;
function strlen($foo) { return htmlentities($foo); }
echo strlen(“test”); // test
echo ::strlen(“test”); // 4
echo namespace::strlen(“test”); // test
Function, class and constant references inside a
namespace refer to namespace first and global scope
later.
6

Namespaces & autoload
__autoload() will be passed namespace name along with
class name.
autoload will only be triggered if class does not exist in
namespace and global scope
__autoload() declared inside a namespace will not be
called!
function __autoload($var) { var_dump($var); } // LIB::foo
require “./ns.php”; /*
<?php
namespace LIB;
new foo();
*/
7

Other NS Syntactic Sugar
namespace really::long::pointlessly::verbose::ns;
__NAMESPACE__; // 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 class from a
namespace
8

Improved Performance
md5() is roughly 10-15% faster
Better stack implementation in the engine
Constants moved to read-only memory
Improve exception handling (simpler & less opcodes)
Eliminate open(2) call on (require/include)_once
Smaller binary size & startup size with gcc4
Overall Improvement 5-15%
9

New Language Features
10

__DIR__
Introduced __DIR__ magic constant indicating the
directory where the script is located.
echo dirname(__FILE__); // = PHP 5.3
11

?: Operator
Allows quick retrieval of a non-empty value from 2
values and/or expressions
$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
12

__callStatic()
__call() equivalent, but for static methods.
class helper {
static function __callStatic($name, $args) {
echo $name.'(‘.implode(‘,’, $args).’)’;
}
}
helper::test(“foo”,”bar”); // test(foo,bar)
. Dynamic function/method calls are kinda slow…
13

Dynamic Static Calls
PHP now allows dynamic calls to static methods
class helper {
static function foo() { echo __METHOD__; }
}
$a = “helper”;
$b = “foo”;
$a::$b(); // helper::foo
. Dynamic function/method calls are kinda slow…
14

Late Static Binding
Processing of static events has been extended into
execution time from compile time.
. Beware if you use an opcode cache
. Not backwards compatible
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
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
15

MySQLInd
Specialized, high speed library to interface with
MySQL designed specifically for PHP
MySQL Database
ext/mysql ext/mysql
Copy of data in PHP
Row Data Row Data Row Data Row Data Row Data Row Data
Copy in libmysql

Copy on Write in
mysqlind
mysql_fetch_assoc(“…”);
16

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s