Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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から予約語がメソッド名として使えるようになっていた件

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away