属性 没有 setter

property does not have a setter

我正在使用 PHP / phalcon 应用程序。我有2份。在服务器 1 中,我没有任何问题。在服务器 2(相同代码)中,我收到以下错误。

property '<property_name>' does not have a setter

因为我有相同的代码,所以我很困惑在这里做什么。我也查看了 php.ini 错误报告,因为这个错误看起来像 php 抱怨我的代码。

但这两个地方我都没有~STRICT

class ClassName { 
    protected $email = null; 
} 

我在外面,

$cls = new ClassName(); 
$cls->email = 'email'; 

在这种情况下,我得到的错误是

property 'email' does not have a setter

protected 变量的全部意义在于限制从 ClassName.

外部直接访问该变量

要访问 protected 变量,您必须使用 getset 函数 extend 您的 ClassName

class ClassName { 
    protected $email = null;

    public function getEmail() {
        return $this->email;
    } 

    public function setEmail($email) {
        $this->email = $email;
    }
} 

您可以按如下方式使用:

$cls = new ClassName(); 
$cls->setEmail('email@example.com');

echo $cls->getEmail(); // outputs: "email@example.com"

如果您不想忙于创建这些 getterssetters,您可以将 protected 变量更改为 public 变量。

旁注:
您确定您的代码 100% 相同吗?
也许您的 error reporting levels?
之间存在不一致 您的 2 个环境中的 PHP 版本是什么?
也许您的 ClassName 拥有(或继承)magic methods __get__set?

__set() is run when writing data to inaccessible properties.
__get() is utilized for reading data from inaccessible properties.

检查您服务器上的 phalcon 版本。我在使用 Phalcon 2.0.13 的本地主机和使用 Phalcon 2.0.6 的服务器上遇到了同样的问题。

我遇到了同样的问题。我改变了我的模型 protected $emailpublic $email,错误消失了。

设置一个魔法怎么样setter? 从 Phalcon 2 升级到 3 时我遇到了同样的问题,并在下面修复了它,而不是手动添加所有 setters。

/**
 * Magic setter function to get rid of 
 * '[Property] does not have a setter' error
 * 
 * @param any value of $field
 * @param any value of $value
 * @return no return
 */

public function __set($field, $value) {
    $this->$field = $value;
}