问答

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

使用一个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",
        ]);
    }

直接继承Repo类, 重新方法, new 类的时候要把 Client 对象传入.

<?php
namespace Base\Bl;
use Github\Api\Repo;
class GithubRepoBl extends Repo
{
    public function readme($username, $repository, $branches ='master', $format = 'raw')
    {
        return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/readme', ['ref' => $branches], [
            'Accept' => "application/vnd.github.$format",
        ]);
    }
}
use Base\Bl\GithubRepoBl;
use Github\Client;
class UserController extends Controller
{
    public function shareProject(Client $client)
    {
        $clientObj = new GithubRepoBl($client);
        $readme = $clientObj->readme('hellomjw', 'pedep', 'lavalite');

    }
}