Can I override the PHP built-in function echo()?

后端 未结 6 1192
無奈伤痛
無奈伤痛 2020-12-11 15:02

I recently looked at my source code and it was a real mess.

my php source:

echo \'

Rar

\'; echo \'Rar\'; e

6条回答
  •  没有蜡笔的小新
    2020-12-11 15:45

    Although this solution does not override echo, you can get something close to echo with a newline. Add:

    function e() {
        return o::singleton();
    }
    
    class o {
        private static $instance;
    
        public static function singleton()
        {
            if (!isset(self::$instance)) {
                $className = __CLASS__;
                self::$instance = new $className;
            }
            return self::$instance;
        }
    
        public function __set($prop, $txt) {
            echo $txt . PHP_EOL;
        }
    }
    

    to your file, and then you can use:

    e()->o = "Line which ends in newline";
    

    instead of echo.

提交回复
热议问题