【问题标题】:Error creating new form elements in Phalcon在 Phalcon 中创建新表单元素时出错
【发布时间】:2015-01-13 10:39:52
【问题描述】:

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

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 版

【问题讨论】:

    标签: php phalcon


    【解决方案1】:

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

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

    【讨论】:

    • 原来我忽略了一些东西,谢谢你的时间。我发布了我的发现。
    【解决方案2】:

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

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

    所以我将代码更改为:

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

    我只是不熟悉=?符号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      相关资源
      最近更新 更多