“未知命名参数”难以理解的错误

"Unknown named parameter" incomprehensible error

提问人:A. Cedano 提问时间:11/6/2023 更新时间:11/6/2023 访问量:46

问:

我正在构建几个非常相似的对象,它适用于所有对象,除了一个。

错误跟踪为:

Fatal error: Uncaught Error: Unknown named parameter $hymnus in /THEROUTE/src/App/Models/Completorium.php:27 Stack trace: 
#0 /THEROUTE/src/App/Domain/Infrastructure/Mapper/Breviarium/PdoBreviariumMapper.php(397): App\Models\Completorium->__construct('completorium', Object(App\Models\Kyrie), Object(App\Models\Hymnus), Object(App\Models\Psalmodia), Object(App\Models\LectioBrevis), Object(App\Models\CanticumEvangelicum), Object(App\Models\LHOratio), Object(App\Models\Conclusio)) 
#1 /THEROUTE/src/App/Domain/Infrastructure/Mapper/Breviarium/PdoBreviariumMapper.php(47): App\Domain\Infrastructure\Mapper\Breviarium\PdoBreviariumMapper->completorium(Object(stdClass)) 
#2 /THEROUTE/src/App/Domain/Infrastructure/Repository/Breviarium/BreviariumRepository.php(21): App\Domain\Infrastructure\Mapper\Breviarium\PdoBreviariumMapper->getData(Array) 
#3 /THEROUTE/src/App/Domain/Service/Breviarium/BreviariumService.php(36): App\Domain\Infrastructure\Repository\Breviarium\BreviariumRepository->getData(Array) 
#4 /THEROUTE/src/App/Controllers/Home.php(44): App\Domain\Service\Breviarium\BreviariumService->getData() 
#5 [internal function]: App\Controllers\Home->getFromServerAction() 
#6 /THEROUTE/src/Core/Controller.php(48): call_user_func_array(Array, Array) 
#7 /THEROUTE/src/Core/Router.php(124): Core\Controller->__call('getFromServer', Array) 
#8 /THEROUTE/index.php(57): Core\Router->dispatch('') 
#9 {main} thrown in /THEROUTE/src/App/Models/Completorium.php on line 27

代码的相关部分:

PdoBreviarium映射器

该行是:,我在以下位置正确传递了一个命名参数:397new \App\Models\Completorium(hymnushymnus: new \App\Models\Hymnus("Lorem ipsum"),

            new \App\Models\Completorium(
                typus: $r->hora,
                kyrie: new \App\Models\Kyrie($r->kyrieID,$r->kyrie),
                hymnus: new \App\Models\Hymnus("Lorem ipsum"),
                psalmodia:
                    new \App\Models\Psalmodia(
                        $r->psalmodiaTypus,
                        (object) [
                            'antiphons' => $r->antiphons,
                            'psalmus' => $r->psalmus,
                            'psalmusPericopa' => $r->psalmusPericopa,
                            'epigraphs' => $r->epigraphs,
                            'themes' => $r->themes,
                            'parts' => $r->parts,
                          ]
                      ),
                lectioBrevis:
                    new \App\Models\LectioBrevis(
                        $r->lectioBrevis,
                        $r->brevisPericopa,
                        new \App\Models\ResponsoriumBrevis(
                            $r->brevisResponsorium,
                            $r->brevisResponsoriumTypus)
                        ),
                canticumEvangelicum:
                    new \App\Models\CanticumEvangelicum(
                    6,
                    array(new \App\Models\Antiphona(0,$r->canticumEvangelicum))),

                oratio:
                    new \App\Models\LHOratio($r->oratio),
                conclusio:
                    new \App\Models\Conclusio($r->antiphonaMariam)

            )

完效

该行是:27typus:$typus,

namespace App\Models;

class Completorium extends \App\Models\LiturgiaTypus implements \JsonSerializable
{
    private Kyrie $kyrie;
    private LectioBrevis $lectioBrevis;
    private CanticumEvangelicum $canticumEvangelicum;
    private Conclusio $conclusio;
    private bool $isPrimaVesperas=false;

    public function __construct
    (
        string $typus,
        Kyrie $kyrie,
        Hymnus $hymnus,
        Psalmodia $psalmodia,
        LectioBrevis $lectioBrevis,
        CanticumEvangelicum $canticumEvangelicum,
        LHOratio $oratio,
        Conclusio $conclusio
    )
    {
        parent::__construct
        (
            typus:$typus,
            hymnus:$hymnus,
            psalmodia:$psalmodia,
            oratio:$oratio
        );

        $this->kyrie=$kyrie;
        $this->lectioBrevis=$lectioBrevis;
        $this->canticumEvangelicum=$canticumEvangelicum;
        $this->conclusio=$conclusio;
    }
    public function jsonSerialize():mixed{
        return get_object_vars($this);
    }
}

赞美诗

namespace App\Models;

class Hymnus implements \JsonSerializable
{
    private string $hymnus;
    public function __construct(string $hymnus)
    {
        $this->hymnus=$hymnus;
    }
    public function jsonSerialize():mixed{
        return get_object_vars($this);
    }
}

我不明白为什么会出现这个错误。我有其他类的类似代码可以正常工作。

php-8 命名参数

评论

0赞 aynber 11/6/2023
我不知道命名参数是否区分大小写。尝试将它们都保持相同的大小写。
0赞 A. Cedano 11/6/2023
@aynber和其他人在类似情况下工作。错误只在这里。lectioBrevis
0赞 Nigel Ren 11/6/2023
我是否可以建议将这一段庞大的代码分解为一种更易于管理的方式 - 如果您在将变量传递给构造函数之前使用各个部分创建变量,那么“行”可能会更容易调试。目前(正如您所发现的)很难知道错误的确切来源。new \App\Models\Completorium(
0赞 A. Cedano 11/6/2023
@NigelRen我明白你的意思。我这样做是为了通过不必要地将对象分配给变量来节省内存。对于调试,我可以检查在这种情况下的内容。$r
0赞 Nigel Ren 11/6/2023
那么,与维护代码所节省的时间相比,您节省了多少内存呢?

答:

1赞 vodevel 11/6/2023 #1

班级呢。它在构造函数中是否有参数?\App\Models\LiturgiaTypushymnus

因为它也可能导致此错误:

parent::__construct
        (
            typus:$typus,
            hymnus:$hymnus,
            psalmodia:$psalmodia,
            oratio:$oratio
        );

评论

1赞 A. Cedano 11/6/2023
天哪,那是个错误!我错误地从另一个在其构造函数中没有参数的类扩展了该类。非常感谢!hymnus