1

Hi there and welcome to my first official problem! I'm trying to override a function, but only certain parts. The function i want to override, is different in several versions of the open source CMS system. The specific parts which i need to change in order for this to work, are the same in all of those versions.

The override has to work with all of those different versions of the CMS system.

I can't use PECL (apd) package or any other external PHP package for this. And as far i know and could find in the PHP manual, there is no function for this.

So, for example:

We have the different CMS system versions: 1, 2 and 3.

And we would have a original function like this one (THIS IS JUST AN EXAMPLE AND NOT THE ACTUAL FUNCTION I NEED CHANGED), which can only be used for version 3. Certain parts are the same in all versions, and some parts are different:

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
    }

And this is what i would like to change for all of those versions:

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;
    }

But the function above (which has changed), will only work with version 3 of the CMS system.

Therefore, is there any way i can only override the specific part i need to change and "get" the code which doesn't have to change some other way so the function would still be executed? So again:

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
    }

Hope you can help me out on this.

1 Answer 1

1

You could create your own middle layer. This is assuming you have access to some variable that defines your current 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;
}

You could also do it with a bit more code reuse:

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @fjoe, thank you very much for your awnser. I used 3 CMS versions and a small function in my example, but what i'm trying to solve, those are about 40 versions and the function i have to override is about 500 lines big. So there is no way for me to achieve it this way, even though you gave a perfectly good awnser!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.