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);
The PostMapper interface and PostMapper does have “Post”
<?php
namespace Core\Model\Mapper;
interface PostMapperInterface
{
public function save(Post $post);
}
I can’t understand why it is complaining about not being a “Post”
It is a
Post, but not thePostit is looking for.You seem to be confused by the namespaces. On one occation,
Postrefers toCore\Model\Mapper\Post, but what you pass is of typeCore\Model\Post.You first state that you are now inside the namespace
Core\Model\Mapper, so when you refer toPostin the method declaration,Postis relative to that namespace, which is why it wants an instance of the typeCore\Model\Mapper\Post.You need to change your code like this: