Perhaps this is my internal bug, but this code was hard for me. At first glance, I estimated the task at 2 minutes. In fact, it turned out 10 - and I am ashamed!

I propose to tell you the time and check for how much you will write a similar task, which displays only logs on the screen.

Similar code: (I wrote in php)

$console->log('program has been started')->('parsing data ... ',false)->log('parsed')->log('finished!'). 

Should output the following:

 out says: program has been started\n out says: parsing data ... parsed\n out says: finished!\n 

Update

 $console->log('parsing ... ',false); //код для парсига или тяжелое что то на 5 сек sleep(5); $console->log('done '); $console->log('Finished '); 

The result will be

out says: parsing ...
done finished

If it is easy, why not write?

  • where is the task itself then? so I wrote js in seconds in console.log ('out says: program has been started'); console.log ('out says: parsing data ... parsed'); console.log ('out says: finished!'); most of all the time it took to copy-paste phrases. On php, this is an object that, in the log method, displays text and returns $ this (apparently $ console is its instance) (what's there - if the second parameter is false, do not insert \ n?) is shorter than garbage and not a task. - zb '
  • one
    banal same problem, I do not understand what the snag is? ! Imgur - etki
  • Why solve a very easy task? - zb 'jul
  • but still a solution? - Sergo
  • You set the flag for the next call log? Well, I said - no wording. But if you set a task so that $ console-> log ('Hello') -> log ('world'); brought out LOG: Hello world; and $ console-> log ('Hello'); $ console-> log ('world'); brought LOG: Hello LOG: World then it would be just a little more interesting. - zb '

1 answer 1

In general, as I said, your task is boring, so I improved it, in general, the solution to the improved task took me more than 10 minutes, because of the gap about the destructor, so:

 <?php class Console { public $lastLineClosed = true; public function __construct($closeLine=true) { $this->lastLineClosed = $closeLine; } public function __destruct() { if (!$this->lastLineClosed) { echo PHP_EOL; } } public function __call($name, $arguments) { //немного больше магии call_user_func_array($name,$arguments); return $this; } public function log($text) { if ($this->lastLineClosed) { echo 'out says: '; $text=ucfirst ($text); } else { $text=" ".$text; } echo $text; if ($this->lastLineClosed) { return new Console(false); } return $this; } } echo "---------\n"; $console = new Console; $console ->log('Hello'); $console ->log('world'); $console ->log('Hello')->log('world'); $console ->log('Hello')->log('world')->magic_run(" magic! ")->log(2); echo "---------\n"; function magic_run($arg) { sleep(3); //это мог бы быть ваш парсер :) echo " -==$arg==-"; } 

http://ideone.com/z5osq3