【问题标题】:Can not implement NormalizableInterface on Symfony 2.0.9无法在 Symfony 2.0.9 上实现 NormalizableInterface
【发布时间】:2012-02-14 10:17:50
【问题描述】:

我正在尝试使用 Symfony 2.0.9 实现 Symfony\Component\Serializer\Normalizer\NormalizableInterface

我添加了这样的实现:

class MRoute implements NormalizableInterface{
...
public function normalize($object, $format = null)
{
     $points = array();
     foreach ($this->getPoints() as $point) {
         $points[] = $point->normalize();
     }
    return array(
        'id' => $this->getId(),
        'name' => $this->getName(),
        'points' => $points

    );
}

/**
 * @see
 */
function denormalize(NormalizerInterface $normalizer, $data, $format = null)
{
    if (isset($data['name']))
    {
        $this->setName($data['name']);
    }

    if (isset($data['id']))
    {
        $this->setId($data['id']);
    }
}

但是当我尝试访问网络服务时,我收到如下错误:

致命错误:MyGIS\GISBundle\Entity\MRoute::normalize() 的声明必须与 C:\NetbeansProjects\mroute_rest_service\src\ 中的 Symfony\Component\Serializer\Normalizer\NormalizableInterface::normalize() 的声明兼容第 16 行的 MyGIS\GISBundle\Entity\MRoute.php

我打开 Symfony\Component\Serializer\Normalizer\NormalizableInterface.php 并且签名匹配。

我做错了什么?

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    v2.0.9 中的界面与您在此处粘贴的完全不同: https://github.com/symfony/symfony/blob/v2.0.9/src/Symfony/Component/Serializer/Normalizer/NormalizableInterface.php

    看起来你已经实现了 2.1 接口,这是完全不同的: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Serializer/Normalizer/NormalizableInterface.php

    如果您实际上使用的是 2.1(主分支),则需要为 NormalizerInterface 添加一个 use 语句。

    use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
    

    【讨论】:

    • 非常感谢。问题是我使用来自 [SymfPony ](symfpony-project.org) 的 Web 服务示例,它必须使用以前的版本。切换签名以与 2.0.9 NormalizableInterface.php 完全匹配,问题就解决了。
    猜你喜欢
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 2017-10-08
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多