Closures
In a nutshell
Anonymous functions that
are declared on-the-fly,
can be assigned to a variable,
passed to other functions,
and remember what happens around them
Functors
In a nutshell
Allow an object to be invoked or called as
if it were an ordinary function
Can be used to implement stateful
callbacks
Also called function objects, functionals or
functionoids
Functors
Usage
<?php
class Example {
public function __invoke() {
print __METHOD__ . "n";
}
}
$object = new Example;
$object();
?>
Example::__invoke
Static Binding
Early Static Binding
<?php
class Base
{
public static function a()
{
print __METHOD__ . "n";
self::b();
}
public static function b()
{
print __METHOD__ . "n";
}
}
class Child extends Base
{
public static function b()
{
print __METHOD__ . "n";
}
}
Child::a();
?>
Base::a
Base::b
Static Binding
Late Static Binding
<?php
class Base
{
public static function a()
{
print __METHOD__ . "n";
static::b();
}
public static function b()
{
print __METHOD__ . "n";
}
}
class Child extends Base
{
public static function b()
{
print __METHOD__ . "n";
}
}
Child::a();
?>
Base::a
Child::b
Namespaces
Declaration
a.php
<?php
namespace project;
const ANSWER = 42;
class SomeClass {}
function do_something() {}
?>
Namespaces
Import from namespace into local scope
a.php
<?php
namespace project;
const ANSWER = 42;
class SomeClass {}
function do_something() {}
?>
c.php
<?php
require 'a.php';
use projectSomeClass;
$object = new SomeClass;
?>
Namespaces
Import from namespace into local scope (conflict)
a.php
<?php
namespace project;
const ANSWER = 42;
class SomeClass {}
function do_something() {}
?>
c.php
<?php
class SomeClass {}
require 'a.php';
use projectSomeClass;
?>
Fatal error: Cannot use projectSomeClass as SomeClass because the
name is already in use in /tmp/c.php on line 6
Namespaces
Import from namespace into local scope with alias
a.php
<?php
namespace project;
const ANSWER = 42;
class SomeClass {}
function do_something() {}
?>
d.php
<?php
class SomeClass {}
require 'a.php';
use projectSomeClass as Foo;
?>
The End
Thank you for your interest!
These slides will be posted on
http://slideshare.net/sebastian_bergmann
License
This presentation material is published under the Attribution-Share Alike 3.0 Unported
license.
You are free:
✔ to Share – to copy, distribute and transmit the work.
✔ to Remix – to adapt the work.
Under the following conditions:
● Attribution. You must attribute the work in the manner specified by the author or
licensor (but not in any way that suggests that they endorse you or your use of the
work).
● Share Alike. If you alter, transform, or build upon this work, you may distribute the
resulting work only under the same, similar or a compatible license.
For any reuse or distribution, you must make clear to others the license terms of this
work.
Any of the above conditions can be waived if you get permission from the copyright
holder.
Nothing in this license impairs or restricts the author's moral rights.