Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* RpbErrorResp
*
* @message RpbErrorResp
*
* -*- magic properties -*-
*
* @property string $errmsg
* @property int $errcode

php-uvで始めるEvent-driven, Nonblocking / IO プログラミング(仮)

対象

  • PHPで開発を行っている中級者向け

概要

普段はApache2等のmodule上で実行されるPHPですが、Node.jsで使われているlibuvをPHPでも使えるようにしたPHP拡張 php-uv を使う事により軽量なイベント駆動やネットワークアプリケーションを簡単に書けるようになります。 本発表ではlibuvの簡単な概要説明からphp-uvを使ったEvent-driven, Nonblocking / IOを使ったプログラミング例の紹介、

var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888,"::1");
<?php
//https://github.com/chobie/php-uv
$loop = uv_default_loop();
$queue = uv_queue_work($loop, function(){
var_dump("[queue]");
}, function(){
var_dump("[finished]");
});
@chobie
chobie / uv.php
Created May 26, 2012 13:37
php-uv exsample
<?php
$server = uv_tcp_init();
uv_tcp_bind($server, "0.0.0.0",5150);
uv_listen($server,1000,function($server){
$client = uv_tcp_init();
uv_accept($server,$client);
uv_read_start($client, function($buffer, $client){
uv_write($client,"Hello\n",function($status, $client){
<?php
namespace Pinatra;
class Application
{
protected static $instance;
public $routes = array();
public static function getInstnace()
{
@chobie
chobie / php-redis-5.4-patch.diff
Created May 4, 2012 02:21
php-redis-5.4-patch
diff --git a/config.m4 b/config.m4
index 51a1cad..f1af506 100755
--- a/config.m4
+++ b/config.m4
@@ -55,5 +55,5 @@ if test "$PHP_REDIS" != "no"; then
dnl
dnl PHP_SUBST(REDIS_SHARED_LIBADD)
- PHP_NEW_EXTENSION(redis, redis.c library.c redis_session.c redis_array.c redis_array_impl.c igbinary/igbinary.c igbinary/hash_si.c igbinary/hash_function.c, $ext_shared)
+ PHP_NEW_EXTENSION(redis, redis.c g_fmt.c library.c redis_session.c redis_array.c redis_array_impl.c igbinary/igbinary.c igbinary/hash_si.c igbinary/hash_function.c, $ext_shared)
@chobie
chobie / niyoniyo.php
Created April 29, 2012 13:32
niyoniyo
<?php
$mruby = new MRuby();
function niyoniyo(MrubyObject $niyo)
{
var_dump($niyo->say);
}
$mruby->run(<<<'EOH'
require 'php'
@chobie
chobie / gist:2493950
Created April 25, 2012 22:19
php-mruby gets started!

php-mruby gets started!

you know mruby is the lightweight implementation of the Ruby language. it also works with php. I made this extension in my lunch time :)

git clone https://github.com/chobie/php-mruby.git --recursive
cd php-mruby
cd mruby
make
<?php
class CustomRender extends Sundown\Render\HTML
{
public function normaltext($paragraph)
{
return nl2br($paragraph);
}
}
$render = new CustomRender();