Duplicating writing in php file

Asked

Viewed 30 times

0

I am implementing a Fileimpl class that contains a write method. However, at the time I write, it seems that the request is duplicated. That is, if I try to add 1 date, as in the example below...

$file = new FileImpl('log.txt');
$file->write(date('d/m/Y H:i:s'));

I have this result in my file writing output:

Output example:

16/04/2019 04:47:58
16/04/2019 04:47:58
16/04/2019 04:54:55
16/04/2019 04:54:55

I really don’t know where the bug is. See the implementation of my class:


class FileImpl {

  private $diretory;
  private $fileName;

  public function __construct(string $fileName, string $directory = NULL) {
    $this->fileName = $fileName;
    if ($directory != NULL)
      $this->diretory = $directory . date('dMY');
  }

  public function write($data) : void {
    if (!$this->diretory) {
      $file = fopen($this->fileName, 'a+');
      echo "Arquivo criado sem diretorio\n";
    } else {
        if (!is_dir($this->diretory)) {
          mkdir($this->diretory);
        }
        $file = fopen($this->diretory . DIRECTORY_SEPARATOR . $this->fileName, 'a+');
        echo "Arquivo criado com diretorio\n";
    }
    fwrite($file, $data."\r\n");
    fclose($file);
  }

}

  • Your code worked perfectly in my test environment. It’s running along with other scripts or it’s running just that?

  • Running only this... Strange...!

  • As it stands, it is impossible to reproduce the problem, and the only thing left is to infer that the problem lies elsewhere in the code. The question is whether you just declare the class and run only the two lines of code you put in the question will still duplicate? In other words, it will be necessary to elaborate a [mcve].

  • This is my whole example class, in this example, I have nothing more than this in my class... always when I upgrade, for test imprint, the line doubles...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.