Getters - Setters

Hello i am new at phalcon. I am trying to undestand how setters and getters work. Let's say i have a class with a protected variable $name. I create a getter and a setter for it. When i have $result = $class->name the getter is called. But when i do $class->name = 'blah' the setter is not called. Why? Why not called always? Is it ok if i implement in a base class the magic function __set the way i want?



84.5k

What's the point of having protected properties that behave as public propeties?



1.3k

Thanx for the reply! I don't want to use them like public properties. I just prefer $class->name = 'blah' (and not $class->setName('blah')) and knowing that my setter will give the protected variable the right value (for example i dont want to have empty string so my setter would make empty string => null). Actually it could be private, that's not the point, it wouldn't matter if a setter would always be called. My question is why not always. I mean what is the logic behind that decision? And why is the getter always called?

the main reasons of using setters/getters vs variables, are scalability and debugging; this is available for all major programming languages and not only php. As your project grows bigger you might need that your $name variable to give you some preprocessed data instead of raw (like prepending it with Mr, Ms etc). If you use setters/getters is easier to change that without modifying your entire code. Also in case of bugs, you can put a breakpoint inside the setter and see the value directly on source. You might have to write more code, but is worth it.



1.3k

Thanx for the reply Stefan. I totally agree and that's what i want to do. I will use getters and setters my question is why phalcon doesn't call my setter in every case. I guess i should just "override" __set in my classes in order to achieve what i want? Why a setter isn't ALWAYS called that is my question. What is the reason behind that fact.