Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHPのstreamで遊ぶ

 PHPのstreamで遊ぶ

#phpblt 4回目で話したネタです

Hiraku NAKANO

April 21, 2016
Tweet

More Decks by Hiraku NAKANO

Other Decks in Technology

Transcript

  1. ίʔυͰݴ͑͹ <?php $fs = fopen('hoge.txt', 'r'); //↑これがストリームリソース $file = new

    SplFileObject('hoge.txt'); //↑内部的にはストリームリソースが存在している
  2. ΍ͬͯΈΔ $ php -a Interactive shell php > stream_wrapper_unregister('file'); php

    > echo file_get_contents('hoge.txt'); PHP Warning: file_get_contents(): file:// wrapper is disabled in the server configuration in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. file_get_contents() php shell code:1
  3. <?php class Aori { private $finished = false; function stream_open($path,

    $mode, $options, &$opened_path) { return true; } function stream_read(int $count) { if ($this->finished) { return false; } $this->finished = true; return 'うるせーバーカ'; } function stream_eof() { return $this->finished; } function stream_stat() { return []; } } stream_wrapper_unregister('file'); stream_wrapper_register('file', 'Aori');
  4. php > require 's1.php'; php > require 's1.php'; うるせーバーカ php

    > require 's'; うるせーバーカ php > echo file_get_contents('hoge'); うるせーバーカ ἤͬͯ͘ΔPHP͕Ͱ͖ͨ