15. PHP 5.4
class MySessionHandler implements SessionHandlerInterface
{
public function open($savePath, $sessionName) {}
public function close() {}
public function read($id) {}
public function write($id, $data) {}
public function destroy($id) {}
public function gc($maxlifetime) {}
}
$handler = new MySessionHandler();
session_set_save_handler($handler, true);
session_start();
16. PHP 5.4
function status() {
$status = session_status();
if($status == PHP_SESSION_DISABLED) {
echo "Session is Disabled";
} else if($status == PHP_SESSION_NONE ) {
echo "Session Enabled but No Session values Created";
} else {
echo "Session Enabled and Session values Created";
}
}
status();
//return Session Enabled but No Session values Created
session_start();
status();
//return Session Enabled and Session values Created
21. PHP 5.4
~/www$ php -S localhost:8080
PHP 5.4.0 Development Server started at Mon Apr
2 11:37:48 2012
Listening on localhost:8080
Document root is /var/www
Press Ctrl-C to quit.
22. PHP 5.4
~/www$ vim server.sh
#! /bin/bash
DOCROOT="/var/www"
HOST=0.0.0.0
PORT=80
ROUTER="/var/www/router.php"
PHP=$(which php)
if [ $? != 0 ] ; then
echo "Unable to find PHP"
exit 1
fi
$PHP -S $HOST:$PORT -t $DOCROOT $ROUTER
24. PHP 5.4
trait File {
public function put($m) {error_log($m, 3, '/tmp/log');}
}
trait Log {
use File;
public function addLog($m) {$this->put('LOG: '.$m);}
}
class Test {
use Log;
public function foo() { $this->addLog('test');}
}
$obj = new Test;
$obj->foo();
//return LOG: test
25. PHP 5.4
trait Game {
public function play() {return "Play Game";}
}
trait Music {
public function play() {return "Play Music";}
}
class Player {
use Game, Music;
}
$o = new Player;
echo $o->play();
Solving confict
PHP does not solve
conflicts automatically
PHP Fatal error: Trait method play has not been applied,
because there are collisions with other trait methods
on Player in /var/www/test/test_traits.php on line 10
26. PHP 5.4
trait Game {
public function play() {return "Play Game";}
}
trait Music {
public function play() {return "Play Music";}
}
class Player {
use Game, Music {
Game::play as gamePlay;
Music::play insteadof Game;
}
}
$o = new Player;
echo $o->play(); //return Play Music
echo $o->gamePlay(); //return Play Game