LoginSignup
12
9

More than 5 years have passed since last update.

PHP7でポエムを書く (あるいは予約語プログラミングの楽しみ)

Last updated at Posted at 2016-01-18

PHP7では予約語プログラミングができるので、こんな感じでポエムを書けます。

このコードはちゃんと実行できます。

<?php

MyClass::new()
    ->if()
    ->abstract()
    ->class()
    ->extends()
    ->echo()
    ->implements()
    ->callable()
    ->interface()
    ->elseif()
    ->global()
    ->function()
    ->continue()
    ->break()
    ->empty()
    ->array()
    ->and()
    ->private()
    ->namespace()
    ->declare()
    ->clone()
    ->as()
    ->eval()
    ->case()
    ->const()
    ->do()
    ->else()
    ->enddeclare()
    ->endfor()
    ->endforeach()
    ->endif()
    ->endswitch()
    ->endwhile()
    ->try()
    ->catch()
    ->finally()
    ->throw()
    ->foreach()
    ->switch()
    ->goto()
    ->instanceof()
    ->insteadof()
    ->isset()
    ->for()
    ->protected()
    ->list()
    ->or()
    ->print()
    ->public()
    ->trait()
    ->return()
    ->default()
    ->static()
    ->unset()
    ->use()
    ->var()
    ->while()
    ->yield()
    ->include()
    ->include_once()
    ->require()
    ->require_once()
    ->final()
    ->exit()
    ->die();

class MyClass
{
    public static function new() { return new static();}
    public function abstract() { return $this; }
    public function and() { return $this; }
    public function array() { return $this; }
    public function as() { return $this; }
    public function break() { return $this; }
    public function callable() { return $this; }
    public function case() { return $this; }
    public function catch() { return $this; }
    public function class() { return $this; }
    public function clone() { return $this; }
    public function const() { return $this; }
    public function continue() { return $this; }
    public function declare() { return $this; }
    public function default() { return $this; }
    public function die() { return $this; }
    public function do() { return $this; }
    public function echo() { return $this; }
    public function else() { return $this; }
    public function elseif() { return $this; }
    public function empty() { return $this; }
    public function enddeclare() { return $this; }
    public function endfor() { return $this; }
    public function endforeach() { return $this; }
    public function endif() { return $this; }
    public function endswitch() { return $this; }
    public function endwhile() { return $this; }
    public function eval() { return $this; }
    public function exit() { return $this; }
    public function extends() { return $this; }
    public function final() { return $this; }
    public function finally() { return $this; }
    public function for() { return $this; }
    public function foreach() { return $this; }
    public function function() { return $this; }
    public function global() { return $this; }
    public function goto() { return $this; }
    public function if() { return $this; }
    public function implements() { return $this; }
    public function include() { return $this; }
    public function include_once() { return $this; }
    public function instanceof() { return $this; }
    public function insteadof() { return $this; }
    public function interface() { return $this; }
    public function isset() { return $this; }
    public function list() { return $this; }
    public function namespace() { return $this; }
    public function or() { return $this; }
    public function print() { return $this; }
    public function private() { return $this; }
    public function protected() { return $this; }
    public function public() { return $this; }
    public function require() { return $this; }
    public function require_once() { return $this; }
    public function return() { return $this; }
    public function static() { return $this; }
    public function switch() { return $this; }
    public function throw() { return $this; }
    public function trait() { return $this; }
    public function try() { return $this; }
    public function unset() { return $this; }
    public function use() { return $this; }
    public function var() { return $this; }
    public function while() { return $this; }
    public function yield() { return $this; }
}

がんばればもっと面白い文が作れるかもしれないので誰か挑戦してみてください。

参考

PHP7から予約語がメソッド名として使えるようになっていた件

12
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
9