Opcache配置

http://php.net/manual/zh/opcache.configuration.php

opcache.validate_timestamps = 1  #设置为1表示会间隔的检查PHP脚本的内容是否有变化, Production设置为0
opcache.revalidate_freq = 0 # 检查脚本时间戳是否有更新的周期,以秒为单位。 设置为 0 会导致针对每个请求, OPcache 都会检查脚本更新.
opcache.memory_consumption = 64 #OPcache 的共享内存大小, 为操作码缓存分配的内存 MB
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 4000  #操作码最多能缓存多少个PHP脚本
opcache.fast_shutdown = 1

面向对象进阶之:closure

/**
 * Class App
 *
 * 使用Closure bindTo实现路由添加.
 */
class App {
  protected $routers = [];
  protected $responseStatus = 200;
  protected $responseType = 'text/html';
  protected $responseBody = 'hello world';

  public function addRoute($routePath, Closure $routeCallback) {
    $this->routers[$routePath] = $routeCallback->bindTo($this, __CLASS__);
    return $this;
  }

  public function dispatch($currentPath = '') {
    if ($currentPath && isset($this->routers[$currentPath])) {
      $this->routers[$currentPath]();
    }

    header('HTTP/1.1 ' . $this->responseStatus);
    header('Content-type: ' . $this->responseType);
    header('Content-length: ' . mb_strlen($this->responseBody));
    print $this->responseBody;
  }

  public static function factory() {
    return new static();
  }
}

App::factory()->addRoute('home', function() {
  $this->responseBody = 'this is front page';
})->addRoute('news', function() {
  $this->responseBody = 'this is news page';
})->dispatch('news');


面向对象进阶之:Interface

/**
 *  使用Interface 实现收集不同源的文本数据.
 */

/**
 * Interface Documentable
 */
Interface Documentable {
  public function getID();
  public function getContent();
}

class DocumentStore {
  protected $data = [];

  public function addDocument(Documentable $document) {
    $key = $document->getId();
    $value = $document->getContent();
    $this->data[$key] = $value;
  }

  public function getDocument() {
    return $this->data;
  }
}

class HtmlDocument implements Documentable{

  protected $url;

  public function __construct($url) {
    $this->url = $url;
  }

  public function getID() {
    return $this->url;
  }

  public function getContent() {
    return file_get_contents($this->url);
  }
}

class StreamDocument implements Documentable {

  protected $resource;
  protected $buffer;

  public function __construct($resource, $buffer = 4096) {
    $this->resource = $resource;
    $this->buffer = $buffer;
  }

  public function getID() {
    return 'resouce-' . (int)$this->resource;
  }

  public function getContent() {
    $streamContent = '';
    rewind($this->resource);
    while (feof($this->resource) === false) {
      $streamContent .= fread($this->resource, $this->buffer);
    }
    return $streamContent;
  }
}

class CommandOuputDocument implements Documentable {

  protected $command;

  public function __construct($command) {
    $this->command = $command;
  }

  public function getID() {
    return $this->command;
  }

  public function getContent() {
    return shell_exec($this->command);
  }
}


$documentStore = new DocumentStore();
$htmldoc = new HtmlDocument('https://www.php.net');
$documentStore->addDocument($htmldoc);

$streamdoc = new StreamDocument();
$documentStore->addDocument($streamdoc);

$commanddoc = new CommandOuputDocument('cat /etc/hosts');
$documentStore->addDocument($commanddoc);

var_dump($documentStore->getDocument());

PHP 面向对象的设计

有句话说,面向对象很难,设计出来可复用的面向对象更难.
我觉得我编程最弱的也就是面向对象,我觉得我没办法找出公有的元素. 我不知道什么属性该设置成Public什么设置成Private. 什么样的函数该写什么样的东西. 我准备好好强化一下这方面的能力. 我买了设计模式这本书, 然后发现这样的书根本不适合我。 不如直接去看别人写的代码.
于是我找到了第一个可以参考的上传类:
https://github.com/aivis/PHP-file-upload-class/blob/master/upload.php

我也自己构建了一个基本的上传类

class Upload {

  protected $max = '2MB';
  protected $upload_path;
  protected $errors = [];
  protected $file;
  protected $newname;
  protected $ext = [];

  public function __construct($file, $upload_path) {
    $this->setFile($file);
    $this->setUploadPath($upload_path);
  }

  protected function setFile() {}
  public function setUploadPath() {}
  public function setMax() {}
  public function setAllowMineType() {}
  public function setNewName() {}
  public function setAllowExt() {}

  public function save() {}

  public static function Factory($file, $upload_dir) {
    return new static($file, $upload_dir);
  }

}

因为之前一直使用Laravel, 所以对Laravel Collection也非常喜欢,代码很优雅,如何处理链式的数组
https://github.com/illuminate/support/blob/master/Collection.php