0

I am using a PHP / phalcon app. I have 2 copies. In server 1, I have no problems. In Server 2 (Same Code) I getting the following error.

property '<property_name>' does not have a setter

Since I have same code I am confused what to do here. I looked into php.ini error reporting as well, Since this error looks like a php complaining about the my code.

But in both places I dont have ~STRICT.

class ClassName { 
    protected $email = null; 
} 

from outside I do,

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

In this case, The error I get is

property 'email' does not have a setter

Timothy
  • 2,004
  • 3
  • 23
  • 29
masterFly
  • 1,072
  • 12
  • 24
  • what's the '' name value actually? – Muhammad Sumon Molla Selim Jul 07 '16 at 18:46
  • `class ClassName { private $email= null; }` from outside I do, `$cls = new ClasasName(); $cls->email = 'email';` In this case, The error I get is `property 'email' does not have a setter` – masterFly Jul 07 '16 at 19:01
  • Just add setter or change visibility to public, don't know why you don't have the same error on 1st server. You defined property it's private. So why i access it outside of class ? Obviously it doesn't work. Learn OOP before using framework. – Juri Jul 07 '16 at 19:05
  • I know. I problem is that how it works in the Server 1. Probably it should be some kind of a server setting. Not sure what. – masterFly Jul 07 '16 at 19:10
  • Sry the variable is `protected`. not `private` – masterFly Jul 07 '16 at 19:16

4 Answers4

1

Сheck phalcon version on your servers. I had the same problem on my local host with Phalcon 2.0.13 and on server I had Phalcon 2.0.6.

  • You were exactly right. Thank you. This error is not there with Phalcon 2.0.10, but it comes with 20.0.13. – masterFly Nov 13 '16 at 16:41
0

The whole point of a protected variable is to restrict direct access to the variable from outside of your ClassName.

To access a protected variable you will have to extend your ClassName with a get or a set function

class ClassName { 
    protected $email = null;

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

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

You can use this as follows:

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

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

If you don't want the hustle of creating these getters and setters you can just change your protected variable to a public variable.

On a side note:
Are you sure your code is 100% the same?
Maybe there is an inconsistancy between your error reporting levels?
What are the PHP versions on your 2 environments?
Maybe your ClassName has ( or inherits ) the magic methods __get and __set?

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

Timothy
  • 2,004
  • 3
  • 23
  • 29
  • This is really strange. I checked the magic methods. I dont have any overridden methods. Does these magic methods to be enabled in PHP or Phalcon or Apache... in order to use them? Please Help – masterFly Jul 31 '16 at 12:53
0

I had the same problem. I change in my model protected $email to public $email and the error disappeared.

Andrés Córdova
  • 345
  • 3
  • 14
0

How about setting up a magic setter ? I had the same problem when upgrading from Phalcon 2 to 3 and got it fixed with below instead of adding all the setters manually.

/**
 * 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;
}
Anton Perera
  • 353
  • 3
  • 9