Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Zły argument
Forum PHP.pl > Forum > Przedszkole
Ruch Radzionków
Witam mam pytanie posiadam taki kod:
app.php
  1. foreach ($argv as $argument)
  2. {
  3. $delete_dash = str_replace("-", "", $argument);
  4. $something->checkAction('getName', $argument);
  5. }

Class ApplicationController:
  1. class ApplicationController
  2. {
  3. public string $argument;
  4. public function __construct($argument)
  5. {
  6. $this->argument = $argument;
  7. }
  8.  
  9. public function checkAction(ActionInterface $argument)
  10. {
  11.  
  12. }

Interface ActionInterface:
  1. interface ActionInterface
  2. {
  3. public function handle(VendingMachineInterface $vendingMachine): ResponseInterface;
  4. public function getName(): string;
  5. }

i podczas użycia $something->checkAction wyskakuje błąd
Fatal error: Uncaught TypeError: VendingMachine\Controller\ApplicationController::checkAction(): Argument #1 ($argument) must be of type VendingMachine\Action\ActionInterface, string given, called in app.php on line 13 and defined in src\Controller\ApplicationController.php:1
5
Stack trace:
#0 app.php(13): VendingMachine\Controller\ApplicationController->checkAction('getName', 'GETA')
#1 {main}
thrown in src\Controller\ApplicationController.php on line 15


jakiej komendy muszę użyć żeby nie wyskakiwał mi błąd ?
aras785
checkAction wymaga ActionInterface więc musisz najpierw stworzyć klasę z która implementuję interface ActionInterface..
Ruch Radzionków
Mam stwożoną klase tylko jej tu nie dodałem
viking
Czym jest u ciebie argv? Bo powinno być tablicą obiektów implementujących interface
aras785
Cytat(Ruch Radzionków @ 18.04.2022, 06:24:45 ) *
Mam stwożoną klase tylko jej tu nie dodałem


nie bardzo rozumiem, czytałeś wgl. kod który tutaj wklejałeś?

w pierwszym skrypcie masz:
  1. ...
  2. $something->checkAction('getName', $argument);
  3. ...

czyli podajesz 2 argumenty do metody checkAction, a niżej masz tę klasę:


  1. class ApplicationController
  2. {
  3. public string $argument;
  4. public function __construct($argument)
  5. {
  6. $this->argument = $argument;
  7. }
  8.  
  9. public function checkAction(ActionInterface $argument)
  10. {
  11.  
  12. }


która przyjmuje ActionInterface, więc skąd u Ciebie wgl. pomysł na wklepanie 2 argumentów? Przecież każdy edytor kodu będę świecił się jak choinka przy pisaniu takiego kodu.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.