php - In a value object, why use specific property set and get methods? -
By looking at a pattern, I know that they meet different sets and property functions, which both write There are boring and many opportunities for typo errors.
Is there a reason to write in this style, rather than the usual received / fixed routine? This is the boilerplate I am using:
Class ValueObject {protected $ property1; Protected $ property2; Protected $ property3; Get public work ($ propname) (if (property_exists ("ValueObject", $ propname)) {$ $ - $ propname}}} Public function set ($ propname, $ value) {if (property_exists ("ValueObject", $ Propname)) {return ($ this-> $ propname = $ value);}}}}
The idea behind the gates and the setters is quite interesting.
Suppose we have a user object with username, first name and last name and age, something like that
class user () {public $ username = 'special!'; Public $ firstname = 'johnny'; public $ Last name = 'difference'; public $ age = 55;} Everything is fine, and assuming that we create a new object inside the $ user variable We're glad to receive and set the $ user-> age name. Now, after the bottom of the line, you have decided that for a particular reason For, you want to determine the age of the user based on a formula, the formula depends on the age of the user itself!
In our small drill, the user's age has decreased his actual age, the length of his name!
You can not modify other methods in your program, they are all wired together, you can not create a new example variable without having to rewrite everything, so what do you do ?
You write a writer from 'go-to' something like
function getAge () {return $ this-> age; } This is trivial, and is boring to write. But now if we need to fix age variables for our entire program, solution is easy to add some code to some recipients Is:
function getAge () {return $ this-> age - Stellen ($ this-> first name); } We do not really want to rewrite anything, only this small part of the code Before you write gates and satars, you also feel that you need them because we humans are terrible in planning the future, and this gives you a great window to add some unplanned code.
Comments
Post a Comment