Getal geregistreerd als string

Overzicht

Sponsored by: Vacatures door Monsterboard

Karina Six

Karina Six

09/02/2022 18:55:59
Anchor link
Hi allen,

De variabele $exemplaren staat geregistreerd als string, ookal heb ik deze aangegeven als integer. Ik heb deze simpelweg op deze manier aangegeven.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
$exemplaren = 0;


Maar ik krijg steeds de volgende foutmelding. Zoals je ziet staat het steeds tussen aanhalingstekens. Hoe geef ik dit op een correcte manier aan?

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
Fatal error: Uncaught TypeError: Film::create(): Argument #3 ($exemplaren) must be of type int, string given, called in /opt/lampp/htdocs/Videotheek/App/Data/FilmDAO.php on line 33 and defined in /opt/lampp/htdocs/Videotheek/App/Entities/Film.php:18 Stack trace: #0 /opt/lampp/htdocs/Videotheek/App/Data/FilmDAO.php(33): Film::create(30, 'Kill Bill', '1') #1 /opt/lampp/htdocs/Videotheek/App/Data/DVDDAO.php(48): FilmDAO->getById(30) #2 /opt/lampp/htdocs/Videotheek/App/Business/DVdbeheer.php(18): DvdDAO->create('0', 30, '0') #3 /opt/lampp/htdocs/Videotheek/App/voegdvdtoe.php(16): DvdService->voegNieuweDvdToe(55, 30, '0') #4 {main} thrown in /opt/lampp/htdocs/Videotheek/App/Entities/Film.php on line 18


Alvast bedankt
 
PHP hulp

PHP hulp

28/03/2024 09:58:47
 
- Ariën  -
Beheerder

- Ariën -

09/02/2022 18:59:10
Anchor link
Hoe gebruik je $exemplaren?
 
Karina Six

Karina Six

09/02/2022 19:04:20
Anchor link
exemplaren is een property van het object film

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
//Entities/Film.php

declare(strict_types = 1);

class Film {
    private static $idMap = array();
    private int $id;
    private string $titel;
    private int $exemplaren;

    public function __construct(int $id, string $titel, int $exemplaren ) {
        $this->id = $id;
        $this->titel = $titel;
        $this->exemplaren = $exemplaren;
    }

    
    public static function create(int $id, string $titel, int $exemplaren){
        if (!isset(self::$idMap[$id])) {
            self::$idMap[$id] = new Film($id, $titel, $exemplaren);
        }

        
        return self::$idMap[$id];
    }

    
    public function getId(): int {
        return $this->id;
    }

    
    public function getTitel() : string{
        return $this->titel;
    }


    public function getExemplaren() : int {
        return $this->exemplaren;
    }

    
    public function setTitel(string $titel) {
        $this->titel = $titel;
    }


    public function setExemplaren(int $exemplaren) {
        $this->exemplaren = $exemplaren;
    }
}

?>


in functie in DAO

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
  
<?php
  public function create(string $titel, int $exemplaren) : Film {
        $sql = "insert into mvc_films (titel, exemplaren) values (:titel, :exemplaren)" ;
        $dbh = new PDO(DBConfig::$DB_CONNSTRING, DBConfig::$DB_USERNAME, DBConfig::$DB_PASSWORD);
        $stmt = $dbh->prepare($sql);
        $stmt->execute(array(':titel' => $titel, ':exemplaren' => $exemplaren));
        $id = $dbh->lastInsertId();
        $film = new Film((int)$id, $titel, $exemplaren);
        $dbh = null;
        return $film;
        }
?>


businesslaag

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
  <?php
  public function voegNieuweFilmToe(string $titel, int $exemplaren) {
        $filmDAO = new FilmDAO();
        $filmDAO->create($titel, $exemplaren);
        }
?>


in de controller
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
<?php
$exemplaren
= 1;

if (isset($_GET["action"]) && ($_GET["action"] === "process")) {
    $filmSvc = new FilmService();
    $filmSvc->voegNieuweFilmToe($_POST["titelTxt"], $exemplaren);
    header("location: toonalledvds.php");
    exit(0);
    
}

?>
Gewijzigd op 09/02/2022 19:10:08 door - Ariën -
 
Rob Doemaarwat

Rob Doemaarwat

09/02/2022 20:41:41
Anchor link
In je foutmelding (trace) staat:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
/opt/lampp/htdocs/Videotheek/App/Data/FilmDAO.php(33): Film::create(30, 'Kill Bill', '1')

Dit laatste '1' klopt dus niet. volg dus de rest van de trace om te zien waar deze string '1' vandaan komt.
 
Ad Fundum

Ad Fundum

11/02/2022 08:56:00
Anchor link
Als je even meekijkt naar de foutmelding, het gaat mis in:
/opt/lampp/htdocs/Videotheek/App/Data/FilmDAO.php op regel 33

Hier staat:
Film::create(30, 'Kill Bill', '1').
Dat gaat niet goed, omdat PHP afdwingt dat het een integer moet zijn vanwege de functiedefinitie van class Film in de regel
public static function create(int $id, string $titel, int $exemplaren) {

Regel 33 van de foutmelding correspondeert met regel 9 in je tweede code snippet van 'functie DAO' (of wat het ook is).
Je zou op basis van die definitie verwachten dat $exemplaren een integer is, vanwege de functiedefinitie waar het in zit:
public function create(string $titel, int $exemplaren) : Film {

Maaaar, static types in PHP wil niet zeggen dat er geen impliciete casting van typen van variabelen plaatsvindt.
Je gebruikt declare(strict_types = 1); , hierover staat in de handleiding op
https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict
alleen iets over functiecalls. Een gemiste kans van PHP naar mijn idee.

Het gaat mis met PDO::execute(). Als je de handleiding leest op https://www.php.net/manual/en/pdostatement.execute.php staat hier "An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR."
Dus moet je PDO vertellen dat je een integer opgeeft. Zie hiervoor voorbeeld #1 in de handleiding.
Gewijzigd op 11/02/2022 08:56:55 door Ad Fundum
 
- Ariën  -
Beheerder

- Ariën -

26/08/2023 09:12:20
Anchor link
.
 
 

Dit topic is gesloten.



Overzicht

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.