Presentation given to the Orange County PHP meetup on Feb 24 2010. The presentation covers the new features in php 5.3 and goes on to show how they are used in Lithium, the most rad php framework.
5. Namespaces
http://php.net/manual/en/language.namespaces.php
• Autoloading made easy
• lithiumcoreLibraries
• li3_docscontrollersBrowserController
• http://groups.google.com/group/php-
standards/web/psr-0-final-proposal
Wednesday, February 24, 2010
6. Namespaces Example
<?php
namespace app;
use appmodelsPost;
class PostsController extends lithiumactionController {
public function index() {
$posts = Post::all();
return compact(‘posts’);
}
}
?>
Wednesday, February 24, 2010
8. Anonymous Functions
http://php.net/manual/en/functions.anonymous.php
• Lambda
• assigned to a variable
• useful for recursion and multiple calls
• Closure
• added as a method parameter
• use() the parent scope
Wednesday, February 24, 2010
9. Lambda Example
<?php
$cube = function ($value) {
return ($value * $value * $value);
};
$result = array_map($cube, array(1, 2, 3));
var_dump($result);
/*
array
0 => int 1
1 => int 8
2 => int 27
*/
?>
Wednesday, February 24, 2010
10. Another Lambda
$multiply = function ($value, $times) use (&$multiply) {
return ($times > 0) ? $multiply($value * $value, --$times) : $value;
};
var_dump($multiply(2, 3));
/*
int 256
*/
Wednesday, February 24, 2010
11. Closure Example
<?php
$data = 'string';
$result = array_filter(array(1, 2, 'string'), function ($value) use ($data) {
return ($value !== $data);
});
var_dump($result);
/*
array
0 => int 1
1 => int 2
*/
?>
Wednesday, February 24, 2010
12. Crazy Example
<?php
function Y($F) {
return current(array(function($f) { return $f($f); }))->__invoke(function($f) use ($F) {
return $F(function($x) use ($f) {
return $f($f)->__invoke($x);
});
});
}
$factorial = Y(function($fact) {
return function($n) use ($fact) {
return ($n <= 1) ? 1 : $n * $fact($n - 1);
};
});
var_dump($factorial(6));
/*
int 720
*/
?>
Wednesday, February 24, 2010
13. Why Static?
• Promotes proper
application design
• Stateless
• Easier to access
Wednesday, February 24, 2010
14. Late Static Binding
http://php.net/lsb
<?php
class A {
public static function who() {
return __CLASS__;
}
public static function test() {
return static::who();
}
}
class B extends A {
public static function who() {
return __CLASS__;
}
}
$result = B::test();
var_dump($result);
/*
string 'B' (length=1)
*/
Wednesday, February 24, 2010
20. ext/sqlite3
• SQLite is a in-process library that implements a
self-contained, serverless, zero-configuration,
transactional SQL database engine (http://www.sqlite.org/
about.html)
• A more compact format for database files.
• Support for both UTF-8 and UTF-16 text.
• Manifest typing and BLOB support.
• New API via Sqlite3 class or sqlite3 functions
Wednesday, February 24, 2010
21. mysqlnd
• mysql native driver
• faster
• easier to compile
• transparent client, same old mysql/mysqli
Wednesday, February 24, 2010
22. Lithium
the most rad php framework
Wednesday, February 24, 2010
23. In Lithium
• Namespaces
• Anonymous Functions
• Late Static Binding
• Syntax Enhancements
• SPL
• Phar
• Sqlite3
Wednesday, February 24, 2010
25. Namespace Example
<?php
namespace appextensionshelper;
class Form extends lithiumtemplatehelperForm {
public function config(array $config = array()) {
....
}
}
Wednesday, February 24, 2010
26. Anonymous Functions
Example
<?php
Validator::add('role', function ($value, $format, $options) {
return (in_array($value, array('admin', 'editor', 'user')));
});
?>
Wednesday, February 24, 2010
27. LSB Example
<?php
namespace lithiumcore;
class StaticObject {
...
}
<?php
?>
namespace lithiumdata;
class Model extends lithiumcoreStaticObject {
...
}
?> <?php
namespace appmodels;
class Post extends lithiumdataModel {
}
?>
Wednesday, February 24, 2010
28. __callStatic
<?php
namespace lithiumdata;
class Model extends lithiumcoreStaticObject {
public static function __callStatic($method, $params) {
...
}
?>
<?php
namespace appcontrollers
use appmodelsPost;
class PostsController extends lithiumactionController {
public function index() {
$posts = Post::all();
return compact('posts')
}
}
?>
Wednesday, February 24, 2010
29. __invoke
<?php
namespace lithiumaction;
class Controller extends lithiumcoreObject {
public function __invoke($request, $dispatchParams, array $options = array()) {
...
}
} <?php
?>
namespace lithiumaction;
class Dispatcher extends lithiumcoreStaticObject {
protected static function _call($callable, $request, $params) {
...
if (is_callable($callable = $params['callable'])) {
return $callable($params['request'], $params['params']);
}
throw new Exception('Result not callable');
...
}
}
?>
Wednesday, February 24, 2010
30. SPL Iterators
<?php
protected function _cleanUp($path = null) {
$path = $path ?: LITHIUM_APP_PATH . '/resources/tmp/tests';
$path = $path[0] !== '/' ? LITHIUM_APP_PATH . '/resources/tmp/' . $path : $path;
if (!is_dir($path)) {
return;
}
$dirs = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($dirs, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $item) {
if ($item->getPathname() === "{$path}/empty") continue;
($item->isDir()) ? rmdir($item->getPathname()) : unlink($item->getPathname());
}
}
?>
Wednesday, February 24, 2010
39. Filters
• Aspect Oriented Programming
secondary or supporting functions are isolated from the main
program's business logic...increase modularity by allowing the
separation of cross-cutting concerns...
(http://en.wikipedia.org/wiki/Aspect-oriented_programming)
• modify core functionality without
extending a class
• define your own callbacks
• @filter
Wednesday, February 24, 2010
40. Routes Filter
<?php
use lithiumactionDispatcher;
Dispatcher::applyFilter('run', function($self, $params, $chain) {
include __DIR__ . '/routes.php';
return $chain->next($self, $params, $chain);
});
?>
Wednesday, February 24, 2010
41. Asset Filter
<?php
use lithiumactionDispatcher;
use lithiumcoreLibraries;
use lithiumnethttpMedia;
Dispatcher::applyFilter('_callable', function($self, $params, $chain) {
list($plugin, $asset) = explode('/', $params['request']->url, 2) + array("", "");
if ($asset && $library = Libraries::get($plugin)) {
$asset = "{$library['path']}/webroot/{$asset}";
if (file_exists($asset)) {
return function () use ($asset) {
$info = pathinfo($asset);
$type = Media::type($info['extension']);
header("Content-type: {$type['content']}");
return file_get_contents($asset);
};
}
}
return $chain->next($self, $params, $chain);
});
?>
Wednesday, February 24, 2010
43. Save Filter
<?php
namespace appmodels;
class Paste extends lithiumdataModel {
public static function __init(array $options = array()) {
parent::__init($options);
static::applyFilter('save', function($self, $params, $chain) {
$document = $params['record'];
if (!$document->id) {
$document->created = date('Y-m-d h:i:s');
}
if (!empty($params['data'])) {
$document->set($params['data']);
}
$document->parsed = $self::parse($document->content, $document->language);
$document->preview = substr($document->content, 0, 100);
$document->modified = date('Y-m-d h:i:s');
$params['record'] = $document;
return $chain->next($self, $params, $chain);
});
}
}
?>
Wednesday, February 24, 2010
44. $_classes
• Dependency Injection
a technique for supplying an external dependency (i.e. a
reference) to a software component - that is, indicating to a part
of a program which other parts it can use...
(http://en.wikipedia.org/wiki/Dependency_injection)
• modify core functionality without
extending a class
Wednesday, February 24, 2010
45. $_classes Example
<?php
namespace lithiumconsole;
class Library extends lithiumconsoleCommand {
...
protected $_classes = array(
'service' => 'lithiumnethttpService',
'response' => 'lithiumconsoleResponse'
);
...
}
?>
Wednesday, February 24, 2010
46. $_classes Example
<?php
namespace lithiumtestscasesconsole;
use lithiumconsoleRequest;
class LibraryTest extends lithiumtestUnit {
...
public function setUp() {
...
$this->classes = array(
'service' => 'lithiumtestsmocksconsolecommandMockLibraryService',
'response' => 'lithiumtestsmocksconsoleMockResponse'
);
...
}
...
public function testArchiveNoLibrary() {
...
$app = new Library(array(
'request' => new Request(), 'classes' => $this->classes
));
$expected = true;
$result = $app->archive();
$this->assertEqual($expected, $result);
}
...
}
?>
Wednesday, February 24, 2010
48. Collections Example
<?php
use lithiumutilCollection;
$coll = new Collection(array('items' => array(0, 1, 2, 3, 4)));
$coll->first(); // 1 (the first non-empty value)
$coll->current(); // 0
$coll->next(); // 1
$coll->next(); // 2
$coll->next(); // 3
<?php
$coll->prev(); // 2
$coll->rewind(); // 0
use lithiumtestGroup;
$coll->each(function($value) {
return $value + 1;
$group = new Group(array('items' => array(
});
'lithiumtestscasescoreLibraries',
$coll->to('array'); // array(1, 2, 3, 4, 5)
'lithiumtestscasescoreObject',
))
?>
$resul = $group->tests()->run();
?>
Wednesday, February 24, 2010
49. More Lithium
• Integrated Test Suite for fast TDD
• Command Line Framework
• Document Based Data Sources
• Object based Record Sets with access
to non static model methods
• Transparent content type rendering
Wednesday, February 24, 2010
50. Still More Lithium
• Automatic output escaping
• Http Services
• g11n for internationalized applications
• Authentication
• Session/Cookie Handling
• Authorization (1.0)
Wednesday, February 24, 2010
51. Even More Lithium
• Validator
• Logging
• Debugger
• Parser
• Inspector
• Sockets
Wednesday, February 24, 2010
52. Lithium Integrations
• Use 3rd party libraries
• Easy to add with Libraries class
• Especially simple when PSR-0 is
followed
• Access classes in a standard way
Wednesday, February 24, 2010
54. Plugins
• namespaces allow for a true plugin
system
• modify application with filters
• add extensions
• share your handy work
Wednesday, February 24, 2010
55. Some Plugins
• li3_docs
• li3_oauth
• li3_bot
• li3_doctrine
• li3_lab
Wednesday, February 24, 2010
57. lithium_qa
• http://rad-dev.org/lithium/wiki/standards
• Check syntax of your code
• Shows rules violations in your code
• Automate the process with SCM hooks
Wednesday, February 24, 2010
58. Lithium
the most rad php framework
• http://lithify.me
• http://rad-dev.org/lithium/wiki
• http://www.ohloh.net/p/lithium
• http://twitter.com/UnionOfRAD
• irc://irc.freenode.net/#li3
• http://search.twitter.com/search?q=%23li3
Wednesday, February 24, 2010