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

php中文网
发布: 2016-06-06 20:07:07
原创
1447人浏览过

示例代码如下:

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这两个属性,如果没有办法那我只能判断定义两个方法:

Fireflies.ai
Fireflies.ai

自动化会议记录和笔记工具,可以帮助你的团队记录、转录、搜索和分析语音对话。

Fireflies.ai 160
查看详情 Fireflies.ai
  • protected function insideGet($propertyName, $default = null); // 提供给内部使用

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

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

立即学习PHP免费学习笔记(深入)”;

回复内容:

示例代码如下:

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); // 提供给外部使用

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

立即学习PHP免费学习笔记(深入)”;

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

你的逻辑思路为:使用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速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号