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

2018-01-31 14:04:35 +08:00
 xoxo419

使用一个 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",
        ]);
    }
1413 次点击
所在节点    问与答
8 条回复
eslizn
2018-01-31 14:12:29 +08:00
下意识想到的就是 di,然而这个库并没有
所以还是老老实实 fork 一份用吧
liyaojian
2018-01-31 15:03:02 +08:00
最简单粗暴的方法,继承 Repo 类重写该方法,获取附加分支参数

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


调用 readme('username','repository','raw','branch')
tmkook
2018-01-31 15:20:55 +08:00
$a = new Repo;
$a->branch = 'master';
public function readme($username, $repository, $format = 'raw')
{
$branch = $this->branch;
}
WuwuGin
2018-01-31 15:42:09 +08:00
在你的项目文件里继承一下然后重写这个方法啊,用 composer 就不知道怎么写了啊。你们 php 都是这么厉害的吗?
xoxo419
2018-01-31 15:44:51 +08:00
@WuwuGin #4 我承认我菜, 你可以说"你真的菜", 但请不要说"你们".
b821025551b
2018-01-31 15:46:28 +08:00
yoke123
2018-01-31 17:13:02 +08:00
有人喷你正常 没人喷你才不正常 毕竟世界上最好的语言
仇恨拉的稳得不行 不存在 OT 行为的
abusizhishen
2018-01-31 18:32:20 +08:00
@WuwuGin 难道你比所有 PHPer 都厉害?你对 PHP 了解多少?你真的明白了楼主是什么意思?还是只想做一个喷子?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/427406

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX