在 Phalcon 中创建新表单元素时出错


Error creating new form elements in Phalcon

尝试创建新的表单元素时出现此错误:

Phalcon''Forms''FileUpload::render() 的声明

必须与 Phalcon''Forms''ElementInterface::render() 的声明兼容

这是我的代码:

namespace Phalcon'Forms;
class FileUpload extends Element implements ElementInterface
{
    public function  __construct(string $name, $options, array $attributes)
    {
    }
    public function render(array $attributes)
    {
        return 'my form element html'; 
    }
}

此时我已经尝试了每种不同的参数表示法,但仍然收到相同的错误。

所以我想知道是否有可能在 Phalcon 中创建这样的表单元素,或者它是否只是我在文档中遗漏的东西。


http://phalcon.agent-j.ru/en/1.3.0/Phalcon/Forms/Element/http://docs.phalconphp.com/en/latest/api/Phalcon_Forms_ElementInterface.html

我使用的是 1.3.2 版

在 phalcon 文档中定义了这个原型:

abstract public string render (array $attributes=?);

所以我将我的代码更改为:

public function render($attributes = false)
{
    return 'my form element html'; 
}

我只是不熟悉=?符号。

你用的是Phalcon 2吗?!在此版本中,应更严格地遵循签名。当我切换到 Phalcon 2 时,我也遇到了一堆这样的错误,但幸运的是,这个版本是 Zephir 编写的,它使源代码更易于阅读。

正如您在 ElementInterface 定义中看到的那样,签名还不太一致,但我们可以很容易地看到render()只有一个参数,没有array类型提示。只需从接口实现中删除 array 类型即可匹配当前签名。