doufu6423 2018-06-14 02:10
浏览 70
已采纳

PHP int类型被解释为float,break page

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?

  • 写回答

4条回答 默认 最新

  • dpmwy80068 2018-06-14 02:15
    关注

    Your ISBN, 98765432123457, is just slightly larger than what fits in an unsigned 32-bit integer. (log 98765432123457 == 32.22376878216809).

    I suspect that as PHP parses your source code, it internalizes that string of digits as a float so as to represent a number larger than what can be encoded as a 32-bit int.

    While ISBNs use only digits, I would not consider them to be numbers. (After all, the first few digits designate a country, some representations have dashes, and some representations have a leading 0.) I would represent this with a String instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序