I'm learning OOP in PHP 7, following an example on a book. On the code
<?php
class Book {
public $isbn;
public $title;
public $author;
public $available;
public function getPrintableTitle() : string {
$result = '<i>'. $this -> title
. '</i> - '. $this -> author;
if (!$this->available) {
$result .= ' <b>Not available</b>';
}
return $result;
}
public function getCopy(): bool{
if ($this -> available < 1) {
return false;
} else {
$this -> available--;
return true;
}
}
public function __construct(
int $isbn,
string $title,
string $author,
int $available
) {
$this -> isbn = $isbn;
$this -> title = $title;
$this -> author = $author;
$this -> available = $available;
}
}
$book = new Book(98765432123457, '1984', 'George Orwell', 12);
if($book->getCopy()) {
echo 'Here, your copy. <br />';
} else {
echo 'Sorry, no copies available for this book. <br />';
}
var_dump($book->isbn);
?>
I get an error that says:
Fatal error: Uncaught TypeError: Argument 1 passed to Book::__construct() must be of the type integer, float given, called in /home/jorge/Learning/php_sandbox/book.php on line 39 and defined in /home/jorge/Learning/php_sandbox/book.php:26 Stack trace: #0 /home/jorge/Learning/php_sandbox/book.php(39): Book->__construct(98765432123457, '1984', 'George Orwell', 12) #1 {main} thrown in /Learning/php_sandbox/book.php on line 26
Tried to search at php online docs, but couldn't find a reason to this error. It seems to me that php is interpreting the int 98765432123457 as a float because of the comma at the end of the statement. If so, is there a way to fix it?