V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
xoxo419
V2EX  ›  问与答

PHP -如何重写这个类的方法?

  •  
  •   xoxo419 · 2018-01-31 14:04:35 +08:00 · 1393 次点击
    这是一个创建于 2288 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用一个 github 接口的 composer 包. 其中有一个获取 readme 的接口少了一个参数[分支]. 如何在不改动代码的情况下来增加一个参数?

    仓库地址 php-github-api

    方法位置-Repo.php

        /**
         * Get the readme content for a repository by its username and repository name.
         *
         * @link http://developer.github.com/v3/repos/contents/#get-the-readme
         *
         * @param string $username   the user who owns the repository
         * @param string $repository the name of the repository
         * @param string $format     one of formats: "raw", "html", or "v3+json"
         *
         * @return string|array the readme content
         */
        public function readme($username, $repository, $format = 'raw')
        {
            return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/readme', [], [
                'Accept' => "application/vnd.github.$format",
            ]);
        }
    
    第 1 条附言  ·  2018-01-31 16:29:19 +08:00
    8 条回复    2018-01-31 18:32:20 +08:00
    eslizn
        1
    eslizn  
       2018-01-31 14:12:29 +08:00
    下意识想到的就是 di,然而这个库并没有
    所以还是老老实实 fork 一份用吧
    liyaojian
        2
    liyaojian  
       2018-01-31 15:03:02 +08:00   ❤️ 1
    最简单粗暴的方法,继承 Repo 类重写该方法,获取附加分支参数

    public function readme($username, $repository, $format = 'raw')
    {
    $branch = func_get_arg(3);
    ...
    }


    调用 readme('username','repository','raw','branch')
    tmkook
        3
    tmkook  
       2018-01-31 15:20:55 +08:00   ❤️ 1
    $a = new Repo;
    $a->branch = 'master';
    public function readme($username, $repository, $format = 'raw')
    {
    $branch = $this->branch;
    }
    WuwuGin
        4
    WuwuGin  
       2018-01-31 15:42:09 +08:00 via Android
    在你的项目文件里继承一下然后重写这个方法啊,用 composer 就不知道怎么写了啊。你们 php 都是这么厉害的吗?
    xoxo419
        5
    xoxo419  
    OP
       2018-01-31 15:44:51 +08:00
    @WuwuGin #4 我承认我菜, 你可以说"你真的菜", 但请不要说"你们".
    b821025551b
        6
    b821025551b  
       2018-01-31 15:46:28 +08:00
    yoke123
        7
    yoke123  
       2018-01-31 17:13:02 +08:00
    有人喷你正常 没人喷你才不正常 毕竟世界上最好的语言
    仇恨拉的稳得不行 不存在 OT 行为的
    abusizhishen
        8
    abusizhishen  
       2018-01-31 18:32:20 +08:00 via Android
    @WuwuGin 难道你比所有 PHPer 都厉害?你对 PHP 了解多少?你真的明白了楼主是什么意思?还是只想做一个喷子?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1519 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:54 · PVG 07:54 · LAX 16:54 · JFK 19:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.