揮発性のメモ2

http://d.hatena.ne.jp/iww/

メッセージキューとオートローディング

メッセージキューに適当なオブジェクトを突っ込んだとき、取り出し時にもちゃんとオートローディングは動作します。


オートローディングされるクラス (TestClass.php)

<?
class TestClass{
    public $text;

    public function hello(){
        print( "$this->text,world\n" );
    }
}
?>


キューに突っ込む側のプログラム (msgsend.php)

#!/usr/bin/php -q
<?
    // オートローディング設定
    spl_autoload_register(function($class){
        print("class is $class\n");
        include "$class.php";
    });

    // キューに突っ込むオブジェクトを用意
    $a = new TestClass;
    $a->text="Hello";

    // キューを作って突っ込む
    $queue = msg_get_queue(123) or die("msg_get_queue");
    msg_send( $queue, 1, $a ) or die("msg_send");
?>


キューから取り出す側のプログラム (msgrecv.php)

#!/usr/bin/php -q
<?
    // オートローディング設定
    spl_autoload_register(function($class){
        print("class is $class\n");
        include "$class.php";
    });

    // キューからオブジェクトを取り出す
    $queue = msg_get_queue(123) or die("msg_get_queue");
    msg_receive( $queue, 0, $mtype, 99999, $a ) or die("msg_receive");

    // ちゃんと復元したか動作させてみる    
    $a->hello();
?>


実行

$ ./msgsend.php
class is TestClass

$ ./msgrecv.php
class is TestClass
Hello,world