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

示例代码如下:

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 **判断如果是类外调用的**,且$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'); // 抛异常

$xiaoMing->getAge(); // 20

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

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

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

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

回复内容:

示例代码如下:

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 **判断如果是类外调用的**,且$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'); // 抛异常

$xiaoMing->getAge(); // 20

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

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

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

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

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

你的逻辑思路为:使用get方法获取属性值,但是年龄需要特别处理,所以不能用get(age),而是应该调用特定的方法getAge。

实际上可以简化为:使用get方法获取属性值,如果该属性需要特别处理则返回指定方法的返回值。

代码示例:

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 **判断如果是类外调用的**,且$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;

}

}

相关标签:php

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值