LoginSignup
4
1

More than 5 years have passed since last update.

Get,GetGetGetWildAndTough! with 嫌なPHP

Posted at

元ネタ

今まで書いた嫌なの

環境

  • PHP7.1.0

嫌なPHP

  • 無名クラス即時実行の中でnull合体演算子使って引数がnullだったら無名クラス定義をして private $and に値を代入しようとした際 __set が呼ばれて Get Wild And してたら Tough
  • 解けない愛のパズルができました
  • (syntax highlightingが効かないのなんで)
<?php

(new class
{
    public function __construct($obj = null)
    {
        $obj = $obj ?? new class
        {
            private $get;
            private $wild;
            private $and;
            private $tough;

            public function __set($k, $v)
            {
                echo ucfirst($v) . PHP_EOL;
                $this->$k = $v;
                if ($k === 'and'
                    && $v === 'and'
                    && $this->get === 'get'
                    && $this->wild === 'wild'
                )
                {
                    echo 'Tough' . PHP_EOL;
                    exit();
                }
            }

            public function getWild()
            {
                return get_object_vars($this);
            }
        };

        $vars = $obj->getWild();
        foreach ($vars as $k => $v) {
            $obj->$k = array_rand($vars);
        }
        self::__construct($obj);
    }
});

一応実行結果

% php getwild.php
Get
Wild
Get
Tough
And
And
Wild
Get
Tough
Get
Wild
And
And
And
Wild
Wild
Wild
And
Get
And
Get
Wild
And
Tough
4
1
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
4
1