xoxo419
V2EX  ›  问与答

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

  •  
  •   xoxo419 · Jan 31, 2018 · 1834 views
    This topic created in 3072 days ago, the information mentioned may be changed or developed.

    使用一个 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",
            ]);
        }
    
    Supplement 1  ·  Jan 31, 2018
    8 replies    2018-01-31 18:32:20 +08:00
    eslizn
        1
    eslizn  
       Jan 31, 2018
    下意识想到的就是 di,然而这个库并没有
    所以还是老老实实 fork 一份用吧
    reexamine
        2
    reexamine  
       Jan 31, 2018   ❤️ 1
    最简单粗暴的方法,继承 Repo 类重写该方法,获取附加分支参数

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


    调用 readme('username','repository','raw','branch')
    tmkook
        3
    tmkook  
    PRO
       Jan 31, 2018   ❤️ 1
    $a = new Repo;
    $a->branch = 'master';
    public function readme($username, $repository, $format = 'raw')
    {
    $branch = $this->branch;
    }
    WuwuGin
        4
    WuwuGin  
       Jan 31, 2018 via Android
    在你的项目文件里继承一下然后重写这个方法啊,用 composer 就不知道怎么写了啊。你们 php 都是这么厉害的吗?
    xoxo419
        5
    xoxo419  
    OP
       Jan 31, 2018
    @WuwuGin #4 我承认我菜, 你可以说"你真的菜", 但请不要说"你们".
    yoke123
        7
    yoke123  
       Jan 31, 2018
    有人喷你正常 没人喷你才不正常 毕竟世界上最好的语言
    仇恨拉的稳得不行 不存在 OT 行为的
    abusizhishen
        8
    abusizhishen  
       Jan 31, 2018 via Android
    @WuwuGin 难道你比所有 PHPer 都厉害?你对 PHP 了解多少?你真的明白了楼主是什么意思?还是只想做一个喷子?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1177 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 17:58 · PVG 01:58 · LAX 10:58 · JFK 13:58
    ♥ Do have faith in what you're doing.