简体   繁体   中英

Type hinting breaks PHP class

I'm encountering this issue. I have set up a class like this:

 <?php

class CalendarTable {
    private $pdo;   
    private $table;

    public function __construct(PDO $pdo, string $table) {
        $this->pdo = $pdo;
        $this->table = $table;

    }



    public function get_next_event_id() {
           [...]
    }



}

But then, when I try to use the class and pass both $pdo and $table, it doesn't work.

If I remove the " string " type hinting from (PDO $pdo, string $table), it works. I am passing a normal string, I have no clue why it's not working (html page breaks). Any idea?

Code using the class:

<?php
            $calendar = new CalendarTable($pdo,'calendar');

Never mind. Here's the reason why it's not working:

Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported. "String" type hinting has been reintroduced with PHP 7.0. I am, using 5.4.45, that's why it doesn't work.

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM