The sample code is as follows: {code ...} now I want to solve the problem: Now I want to unify the internal attributes of the category class through the get method. In the get method, how can I access the $ name, $ hi, $ age attributes inside the class, only $ name and $ hi attributes can be accessed outside the class. If there is no way, I only... the sample code is as follows:
Class Person {protected $ name; protected $ hi; protected $ age; public function _ construct ($ name, $ hi, $ age) {$ this-> name = $ name; $ this-> hi = $ hi; $ this-> age = $ age;} public function get ($ propertyName, $ default = null) {if (...) {// TODO ** determines if the call is out-of-class ** and $ propertyName = 'age' throw new \ InvalidArgumentException (spintf ('% s access failed! ', $ PropertyName);} if (isset ($ this-> $ propertyName) & $ this-> $ propertyName! = Null) {return $ this-> $ propertyName;} else {return $ default;} public function getAge () {return $ this-> get ('age ', 18) ;}}$ xiaoMing = new Person ('xiaoming', 'I \'m xiaoming', 20); $ xiaoMing-> get ('age '); // throw an exception $ xiaoMing-> getAge (); // 20
Now I want to solve the problem: Now I want to unify the internal attributes of the category class through the get method. In the get method, how can I access the $ name, $ hi, $ age attributes inside the class, only $ name and $ hi attributes can be accessed outside the class. If there is no way, I can only define two methods:
Protected function insideGet ($ propertyName, $ default = null); // It is provided for internal use.
Public function outsideGet ($ propertyName, $ default = null); // It is provided for external use.
Please help me solve this problem. If there is a better bill, please let me know. Thank you.
Reply content:
The sample code is as follows:
Class Person {protected $ name; protected $ hi; protected $ age; public function _ construct ($ name, $ hi, $ age) {$ this-> name = $ name; $ this-> hi = $ hi; $ this-> age = $ age;} public function get ($ propertyName, $ default = null) {if (...) {// TODO ** determines if the call is out-of-class ** and $ propertyName = 'age' throw new \ InvalidArgumentException (spintf ('% s access failed! ', $ PropertyName);} if (isset ($ this-> $ propertyName) & $ this-> $ propertyName! = Null) {return $ this-> $ propertyName;} else {return $ default;} public function getAge () {return $ this-> get ('age ', 18) ;}}$ xiaoMing = new Person ('xiaoming', 'I \'m xiaoming', 20); $ xiaoMing-> get ('age '); // throw an exception $ xiaoMing-> getAge (); // 20
Now I want to solve the problem: Now I want to unify the internal attributes of the category class through the get method. In the get method, how can I access the $ name, $ hi, $ age attributes inside the class, only $ name and $ hi attributes can be accessed outside the class. If there is no way, I can only define two methods:
Protected function insideGet ($ propertyName, $ default = null); // It is provided for internal use.
Public function outsideGet ($ propertyName, $ default = null); // It is provided for external use.
Please help me solve this problem. If there is a better bill, please let me know. Thank you.
I don't know how to check the caller, but I usually don't need to do this.
Your logic is to use the get method to obtain the property value, but the age must be specially processed. Therefore, you cannot use get (age). Instead, you should call the specific method getAge.
In fact, it can be simplified to using the get method to obtain the attribute value. If this attribute requires special processing, the return value of the specified method is returned.
Sample Code:
public function get($propertyName, $default = null) { if (method_exists($this, '_get' . $propertyName)) { $method = '_get' . $propertyName; return $this->$method($default); } if (isset($this->$propertyName) && $this->$propertyName !== null) { return $this->$propertyName; } return $default;}protected function _getAge() { if (! isset($this->age)) { $this->age = 18; } return $this->age;}
Public function get ($ propertyName, $ default = null) {if (...) {// TODO ** determines if the call is out-of-class ** and $ propertyName = 'age' throw new \ InvalidArgumentException (spintf ('% s access failed! ', $ PropertyName);} return getInner ($ propertyName, $ default);} protected function getInner ($ propertyName, $ default = null) {if (isset ($ this-> $ propertyName) & $ this-> $ propertyName! = Null) {return $ this-> $ propertyName;} else {return $ default ;}}