2017-08-27 5 views
1

こんにちは、私の最初の公式の問題へようこそ!私は関数をオーバーライドしようとしていますが、特定の部分のみをオーバーライドしています上書きしたい機能は、オープンソースのCMSシステムのいくつかのバージョンでは異なります。これを動作させるために変更する必要がある特定の部分は、これらのすべてのバージョンで同じです。PHPは、元の関数の特定の部分に対してのみオーバーライド関数を作成します

オーバーライドは、CMSシステムの異なるバージョンすべてで機能する必要があります。

PECL(apd)パッケージまたはこれ以外の外部PHPパッケージを使用することはできません。そして、私が知っている限り、PHPのマニュアルで見つけることができるように、これのための機能はありません。

ので、例えば:

我々は、さまざまなCMSシステムのバージョンがあります:1、2、3

をそして、我々は、これは単なる一例であり、NOT(このような本来の機能を持っているでしょう唯一のバージョン3.特定の部品を使用することができ、私はCHANGED NEED実際の関数)は、すべてのバージョンで同じであり、いくつかの部分が異なっている:

public function createAccessToken($body = false) 
    { 
     if (!$body) { //this is different 
      $body = 'grant_type=client_credentials'; //this is different 
     } 
     $this->action = 'POST'; //this is different 
     $this->endpoint = 'v1/oauth2/token'; //this is different 
     $response = $this->makeCall($body, "application/json", true); //this is different 
     if (!isset($response->access_token)) { //this is THE SAME 
      return false; //this is THE SAME 
     } 
     $this->token = $response->access_token; //this is different 
     return true; //this is different 
    } 

そして、これは私がすべてのために変更したいものですそれらのバージョン:

public function createAccessToken($body = false) 
    { 
     if (!$body) { 
      $body = 'grant_type=client_credentials'; 
     } 
     $this->action = 'POST'; 
     $this->endpoint = 'v1/oauth2/token'; 
     $response = $this->makeCall($body, "application/json", true); 
     if (isset($response->access_token)) { //IT'S CHANGED! THE -> ! IS GONE 
      return false; 
     } 
     $this->token = $response->access_token; 
     return true; 
    } 

しかし、(変更された)上記の機能は、CMSシステムのバージョン3でのみ機能します。

したがって、変更する必要がある特定の部分をオーバーライドできるだけの方法がありますか?関数がまだ実行されるように、他の方法で変更する必要のないコードを「取得」しますか?もう一度:

public function createAccessToken($body = false) 
    { 
     //some way to get the code above the part i need to change, untill i get to the part which needs to be changed. So the override "takes over" from here. 

     if (isset($response->access_token)) { //IT'S CHANGED! THE -> ! IS GONE 
      return false; 
     } 
     //some way to get the rest of the code and have the function continue agian 
    } 

あなたは私にこれを助けることができますように。

答えて

1

独自の中間層を作成できます。これは、現在のVERSIONを定義する変数にアクセスできることを前提としています。あなたはまた、もう少しコードの再利用でそれを行うことができ

public function createAccessToken($body = false) { 
    if (VERSION == 1 || VERSION == 2) { createAccessTokenOld($body); } 
    else { createAccessTokenNew($body); } 
} 

public function createAccessTokenOld($body = false) 
{ 
    if (!$body) { 
     $body = 'grant_type=client_credentials'; 
    } 
    $this->action = 'POST'; 
    $this->endpoint = 'v1/oauth2/token'; 
    $response = $this->makeCall($body, "application/json", true); 
    if (!isset($response->access_token)) { //IT'S CHANGED! THE -> ! IS GONE 
     return false; 
    } 
    $this->token = $response->access_token; 
    return true; 
} 

public function createAccessTokenNew($body = false) 
{ 
    if (!$body) { 
     $body = 'grant_type=client_credentials'; 
    } 
    $this->action = 'POST'; 
    $this->endpoint = 'v1/oauth2/token'; 
    $response = $this->makeCall($body, "application/json", true); 
    if (isset($response->access_token)) { //IT'S CHANGED! THE -> ! IS GONE 
     return false; 
    } 
    $this->token = $response->access_token; 
    return true; 
} 

public function createAccessToken($body = false) { 
    if (VERSION == 1 || VERSION == 2) { createAccessToken($body, true); } 
    else { createAccessToken($body, false); } 
} 

public function createAccessToken($body = false, $isOld = false) 
{ 
    if (!$body) { 
     $body = 'grant_type=client_credentials'; 
    } 
    $this->action = 'POST'; 
    $this->endpoint = 'v1/oauth2/token'; 
    $response = $this->makeCall($body, "application/json", true); 

    if ($isOld) { if (!isset($response->access_token)) { return false; } } 
    else { if (isset($response->access_token)) { return false; } } 

    $this->token = $response->access_token; 
    return true; 
} 
+0

@fjoeこんにちは、あなたのawnserためにどうもありがとうございました。私は3つのCMSバージョンと私の例では小さな関数を使用しましたが、私が解決しようとしているのは約40バージョンです。あなたが完全に良い殻を与えたにもかかわらず、このように私が達成する方法はありません! – Mandy

関連する問題