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 the Post
it is looking for.
You seem to be confused by the namespaces. On one occation, Post
refers to Core\Model\Mapper\Post
, but what you pass is of type Core\Model\Post
.
namespace Core\Model\Mapper;
interface PostMapperInterface
{
public function save(Post $post);
}
You first state that you are now inside the namespace Core\Model\Mapper
, so when you refer to Post
in the method declaration, Post
is relative to that namespace, which is why it wants an instance of the type Core\Model\Mapper\Post
.
You need to change your code like this:
public function save(\Core\Model\Post $post);