I want to declare method in child class as static or non static depend on parent class.
I’m using php and due to change in version I’m getting this issue can anyone please help?
here is example that demonstrate what exactly has changed
//in earlier version
class parent{
function test(){
//some code here
}
}
class child extends parent{
function test(){
//some code here
}
}
//in new version
class parent{
static function test(){
//some code here
}
}
class child extends parent{
function test(){
//some code here
}
}
Fatal error: Cannot make static method parent::test() non static in class child
I want my code to be compatible with both version.what should i do?
Preface: This is a really bad idea. If your base framework had such a fundamental change between two versions that the API has broken backwards compatibility, you should make two separate versions of your class targeting the old and new version separately. It’s most likely not simply the function signature that changed to
static, you’ll most likely have to change a lot of code to work with that newstaticway of doing things.To answer your literal question though: You cannot make one class which is compatible with both, since the change is simply incompatible. You can dynamically declare a different class depending on which version you’re targeting though. If the base framework doesn’t have an obvious version number you can read, you can introspect the parent class:
Again, this is a pretty bad idea due to the code duplication under the same name. But it’s the only choice.