【问题标题】:PHP 7 Fatal error: Uncaught TypeError: Argument 1 passed to Some2::send() must be an instance of Path\To, instance of Path\To\Some givenPHP 7 致命错误:未捕获的类型错误:传递给 Some2::send() 的参数 1 必须是 Path\To 的实例,Path\To\Some 的实例
【发布时间】:2021-04-16 13:11:24
【问题描述】:

我有 3 个 .php 文件:some_class.php、some2_class.php 和 index.php

some_class.php

<?php
namespace Path\To;

class Some {
    public $id;
    public function __construct() {
        $this -> id = 2021;
    }
}

some2_class.php

<?php
class Some2 {
    public function send(\Path\To $some)
    {
        
    }
}

index.php

require_once('some_class.php');
require_once('some2_class.php');

$c = new Path\To\Some();

$c2 = new Some2();
$c2 -> send($c);

当执行 index.php 时,我看到了:

致命错误:未捕获的类型错误:参数 1 传递给 Some2::send() 必须是 Path\To 的实例,Path\To\Some 的实例给定,称为 在第 8 行的 /home/l/liketeks/work/public_html/index.php 中并定义 在 /home/l/liketeks/work/public_html/some2_class.php:3 堆栈跟踪:#0 /home/l/liketeks/work/public_html/index.php(8): Some2->send(Object(Path\To\Some)) #1 {main} 抛出 /home/l/liketeks/work/public_html/some2_class.php 在第 3 行

【问题讨论】:

  • 您对此有何疑问?为什么不修复该消息告诉您的内容?

标签: php


【解决方案1】:

我认为您缺少类型提示的完整路径。不应该是:

class Some2 {
    public function send(\Path\To\Some $some)
    {
        
    }
}

【讨论】:

    猜你喜欢
    • 2014-01-29
    • 2013-11-23
    • 2023-03-30
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多