A public method is defined in the php class. How can I know whether this method is called outside the class or in the class?

Source: Internet
Author: User
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 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.