简体   繁体   中英

Conditional Declaring of Extending Class (Overwriting) Method, Based on the Parent Method's Default Parameters

I am creating a plugin for a CMS that provides a few base classes (let's say one of these classes is called Base ). This class has a few helper methods that must be overwritten in the extending class. We should note that the base methods have default parameters/values provided. In one version of the LMS these values are provided by reference in the next version just by value.

For example (CMS v1.0):

function prepareTable(&$table){...

CMS v1.1:

function prepareTable($table){...

When you extend the Base class and overwrite the prepareTable method you have to declare it with the same default parameters/values as well, otherwise a STRICT PHP warning is displayed (on by default in PHP 5.4).

My question is, how do I conditionally overwrite the method from the parent class in a working way, knowing the version of the parent CMS?

Here's what I have currently (not working at the moment):

class Base{
    function prepareTable(&$table){
    }
}

class Extending extends Base{
    if(CMS_VERSION=='1.0')
      function prepareTable(&$table){
    else
      function prepareTable($table){

       echo $table;
    }
}

Obviously, I can not edit the Base and its method directly.

EDIT: Here's the exact error message:

Strict standards: Declaration of Extending::prepareTable() should be compatible with Base::prepareTable($table) in.

the only way I can think of achieving this without duplicating the code inside prepareTable is to create a pseudo function that gets called inside prepareTable and then declare that in the final extended class

if(CMS_VERSION=='1.0') {
    class Base2 extends Base{
        function prepareTable(&$table){
            return $this->prepareTable2($table);
        }
        function prepareTable2(&$table){
        }
    }
} else {
    class Base2 extends Base{
        function prepareTable($table){
            return $this->prepareTable2($table);
        }
        function prepareTable2(&$table){
        }
    }
}
class Extending extends Base2{
    function prepareTable2(&$table){
            echo $table;
    }
}
if(CMS_VERSION=='1.0') {
    class Extending extends Base{
        function prepareTable(&$table){

        }
    }
} else {
    class Extending extends Base{
        function prepareTable($table){

        }
    }
}

Note that the if/else check must be done before the class is defined, not inside the class. Essentially, you are building two different versions of the class.

Side Note: If you need to include shared methods, that won't be changed between the two versions of the class, you can define a new class that will extend Extending , create the shared methods there and use this new class.

For example (place this after the code above):

class ExtendingFull extends Extending{
    // Here you may include your shared methods
    // e.g:
    public function sharedMethod(){
       echo 'test';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM