当前位置:Gxlcms > PHP教程 > php类中定义了一个public的方法,怎么知道这个方法是被类外调用的还是类内调用的

php类中定义了一个public的方法,怎么知道这个方法是被类外调用的还是类内调用的

时间:2021-07-01 10:21:17 帮助过:5人阅读

示例代码如下:

  1. <code>class Person
  2. {
  3. protected $name;
  4. protected $hi;
  5. protected $age;
  6. public function __construct($name, $hi, $age)
  7. {
  8. $this->name = $name;
  9. $this->hi = $hi;
  10. $this->age = $age;
  11. }
  12. public function get($propertyName, $default = null)
  13. {
  14. if (...) { // TODO **判断如果是类外调用的**,且$propertyName === 'age'
  15. throw new \InvalidArgumentException(spintf(
  16. '%s access failed!',
  17. $propertyName
  18. ));
  19. }
  20. if (isset($this->$propertyName) && $this->$propertyName !== null) {
  21. return $this->$propertyName;
  22. } else {
  23. return $default;
  24. }
  25. }
  26. public function getAge()
  27. {
  28. return $this->get('age', 18);
  29. }
  30. }
  31. $xiaoMing = new Person('xiaoMing', 'I\'m xiaoMing', 20);
  32. $xiaoMing->get('age'); // 抛异常
  33. $xiaoMing->getAge(); // 20
  34. </code>

现在我想解决的问题是:现在我想访问类内部属性统一通过get方法,在get方法里面类内部可以访问怎么$name, $hi, $age三个属性,类外部只能访问$name, $hi这两个属性,如果没有办法那我只能判断定义两个方法:

  • protected function insideGet($propertyName, $default = null); // 提供给内部使用

  • public function outsideGet($propertyName, $default = null); // 提供给外部使用

请大神帮忙解决这个问题,如果有更好的法案也请告知,谢谢

回复内容:

示例代码如下:

  1. <code>class Person
  2. {
  3. protected $name;
  4. protected $hi;
  5. protected $age;
  6. public function __construct($name, $hi, $age)
  7. {
  8. $this->name = $name;
  9. $this->hi = $hi;
  10. $this->age = $age;
  11. }
  12. public function get($propertyName, $default = null)
  13. {
  14. if (...) { // TODO **判断如果是类外调用的**,且$propertyName === 'age'
  15. throw new \InvalidArgumentException(spintf(
  16. '%s access failed!',
  17. $propertyName
  18. ));
  19. }
  20. if (isset($this->$propertyName) && $this->$propertyName !== null) {
  21. return $this->$propertyName;
  22. } else {
  23. return $default;
  24. }
  25. }
  26. public function getAge()
  27. {
  28. return $this->get('age', 18);
  29. }
  30. }
  31. $xiaoMing = new Person('xiaoMing', 'I\'m xiaoMing', 20);
  32. $xiaoMing->get('age'); // 抛异常
  33. $xiaoMing->getAge(); // 20
  34. </code>

现在我想解决的问题是:现在我想访问类内部属性统一通过get方法,在get方法里面类内部可以访问怎么$name, $hi, $age三个属性,类外部只能访问$name, $hi这两个属性,如果没有办法那我只能判断定义两个方法:

  • protected function insideGet($propertyName, $default = null); // 提供给内部使用

  • public function outsideGet($propertyName, $default = null); // 提供给外部使用

请大神帮忙解决这个问题,如果有更好的法案也请告知,谢谢

不知道怎么检查调用者,但是通常根本不需要这样处理。

你的逻辑思路为:使用get方法获取属性值,但是年龄需要特别处理,所以不能用get(age),而是应该调用特定的方法getAge。
实际上可以简化为:使用get方法获取属性值,如果该属性需要特别处理则返回指定方法的返回值。
代码示例:

  1. <code>public function get($propertyName, $default = null) {
  2. if (method_exists($this, '_get' . $propertyName)) {
  3. $method = '_get' . $propertyName;
  4. return $this->$method($default);
  5. }
  6. if (isset($this->$propertyName) && $this->$propertyName !== null) {
  7. return $this->$propertyName;
  8. }
  9. return $default;
  10. }
  11. protected function _getAge() {
  12. if (! isset($this->age)) {
  13. $this->age = 18;
  14. }
  15. return $this->age;
  16. }</code>

  1. <code>public function get($propertyName, $default = null)
  2. {
  3. if (...) { // TODO **判断如果是类外调用的**,且$propertyName === 'age'
  4. throw new \InvalidArgumentException(spintf(
  5. '%s access failed!',
  6. $propertyName
  7. ));
  8. }
  9. return getInner($propertyName, $default);
  10. }
  11. protected function getInner($propertyName, $default = null)
  12. {
  13. if (isset($this->$propertyName) && $this->$propertyName !== null) {
  14. return $this->$propertyName;
  15. } else {
  16. return $default;
  17. }
  18. }</code>

人气教程排行