1

I have this code...

class log{
    private $dateTime;
    private $message;

    function log($dt, $message){
        $this->dateTime = DateTime::createFromFormat('d/m/Y H:i:s', substr($dt, 0, -1)); 
        $this->message = $message;
    }
    function get_dateTime(){
        return $this->dateTime->format('d/m/Y H:i:s');
    }
    function get_message(){
        return $this->message;
    }
}

In parameter $dt has format 17/05/2019 03:15:42:. I get it by reading a text file.

I read the file and create the class object:

while (($line = fgets($handle)) !== false && $line !="\n") { //format example : 30/04/2019 07:59:14: File creation.
   $date = strtok($line, " ");
   $hour = strtok(" ");
   $message = strtok("");

   $log = new log($date." ".$hour,$message);
   echo $log->get_dateTime()."<br>";
}

The result is: Fatal error: Call to a member function format() on a non-object in...

If I pass the parameter "manually" it works fine.

$log2 = new log('30/04/2019 07:59:14:',"test");
echo $log2->get_dateTime()."<br>";

If I use code echo $date; Correct date is returned. Same if I make echo $hour;

How is this possible?

jonal
  • 55
  • 9
  • Are you sure that `$dt` has a trailing colon? `var_dump($dt);` and `var_dump($this->dateTime);` in your log function to see what each contains. – aynber Jul 18 '19 at 16:50
  • Note, constructors named to match your class have been deprecated since 2015, they should be named `__construct()` instead. – Alex Howansky Jul 18 '19 at 16:50
  • Because format readed from file is ```30/04/2019 07:59:14:``` So I remove ```:```. – jonal Jul 18 '19 at 16:50
  • Same result if I use ```__construct()```. Thanks for the information. – jonal Jul 18 '19 at 16:52
  • Please do a `var_dump($date, $hour);` and post the results. We need to see what the variables actually contains (since we can't debug your code ourselves). – M. Eriksson Jul 18 '19 at 16:57
  • ```echo var_dump($date,$hour);``` returns ```string(13) "30/04/2019" string(9) "07:59:14:"``` I do not understand it. Date should be string(10), right? – jonal Jul 18 '19 at 17:02
  • There must be some row in the file you're iterating through that has a different format. Tested your code (changed the constructor to `__construct()` instead of `log()` since that throws a deprecation warning) and it seem to work just fine: [Here's a demo](https://3v4l.org/dl2G2) – M. Eriksson Jul 18 '19 at 17:07
  • Maybe any special char like ```/r``` or similar makes string larger? – jonal Jul 18 '19 at 17:18
  • [link](https://stackoverflow.com/questions/28102962/why-would-var-dump-return-a-bigger-value-than-the-string-length) it seems to be because file encoding. ```string(23) "30/04/2019 07:59:14:" ->ef bb bf 33 30 2f 30 34 2f 32 30 31 39 20 30 37 3a 35 39 3a 31 34 3a string(20) "30/04/2019 07:59:14:" ->33 30 2f 30 34 2f 32 30 31 39 20 30 37 3a 35 39 3a 31 34 3a``` – jonal Jul 18 '19 at 17:37

1 Answers1

0

Problems seems to be the file encoing. When checking line section in hexadecimal it seems to use 3 byte more than when passing text manually.

file seccion in HEX: ef bb bf 33 30 2f 30 34 2f 32 30 31 39 20 30 37 3a 35 39 3a 31 34 3a

same text in HEX passed manually: 33 30 2f 30 34 2f 32 30 31 39 20 30 37 3a 35 39 3a 31 34 3a

Also string size is different (string(23) and string(20)) checking it with var_dump function.

File is in UTF8 encoding. https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8

jonal
  • 55
  • 9