I have a single PHP class, which can communicate with an API we are developing. Rather than using different API versions and letting the user manually updating the PHP class script, I would like to make this process automatic.
This update has to be done on the fly, when an update has been found. This is my idea:
- The user's program tries to access the API via the PHP class we provide
- The PHP class checks for an update
- If there is an update, the class would download the new version of the class, and would like the new version of the class to handle the API request.
This obviously means that the PHP class needs write permissions to the file the class exists in, so it can simply overwrite the class with the new version.
But how can the old class now execute the requested API request through the new class version? In a perfect world, I'm looking for a way to do this without the use of eval(), as this function is blocked on many hosts.
To elaborate:
$myApi = new MyApi;
$myApi->registerCustomer($customerData);
The registerCustomer() function would do something like this:
if (classNotUpToDate) {
downloadNewClass();
registerCustomerthroughNewClass()
} else {
registerCustomerDo();
}
Now the only way I can think of to do this:
- Download the new class version into a variable
- In that variable, replace
class MyApi {...
withclass MyApiUpdate {..
- Include the current file again, loading the new, updated class
- Create an instance of the new class:
$myApiUpdate = new MyApiUpdate;
- Invoke the
registerCustomer()
function:$myApiUpdate->registerCustomer($customerData);
- Overwrite the current file contents with the new class code (without replacements)
Is this the only way to achieve what I want without either creating a new file or using eval()? I don't think this is a very handsome method, so I'm looking for a cleaner way to achieve this.