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 15index.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);
}我不明白为什么它会抱怨自己不是“邮报”
发布于 2012-12-09 02:08:42
它是一个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);https://stackoverflow.com/questions/13780284
复制相似问题