PSR-0论证通过

时间:2012-12-08 18:06:51

标签: php psr-0

Catchable fatal error: Argument 1 passed to Core\Model\Mapper\PostMapper::save() must be an instance of Core\Model\Mapper\Post, instance of Core\Model\Post given, called in C:\wamp\www\Test\index.php on line 16 and defined in C:\wamp\www\Test\Core\Model\Mapper\PostMapper.php on line 15

的index.php

<?php
require_once 'Core/Library/SplClassLoader.php';

$loader = new SplClassLoader('Core', '');
$loader->register();

use Core\Model\Post,
    Core\Model\Mapper\PostMapper;

$db = false;

$postMapper = new PostMapper($db);

$post = new Post;

$postMapper->save($post);

PostMapper接口和PostMapper确实有“Post”

<?php
namespace Core\Model\Mapper;

interface PostMapperInterface
{

    public function save(Post $post);
}

我无法理解为什么抱怨不是“邮报”

1 个答案:

答案 0 :(得分:1)

这是Post,但不是它正在寻找的Post

您似乎对命名空间感到困惑。在一个视频中,Post表示Core\Model\Mapper\Post,但您传递的是Core\Model\Post类型。

namespace Core\Model\Mapper;

interface PostMapperInterface
{

    public function save(Post $post);
}

您首先声明您现在位于命名空间Core\Model\Mapper内,因此当您在方法声明中引用Post时,Post 与该命名空间相关,这就是为什么它想要一个Core\Model\Mapper\Post类型的实例。

您需要像这样更改代码:

public function save(\Core\Model\Post $post);