从子类调用函数[关闭]

I have a feeling that what I am trying to do is impossible as I can't find anything on it. I have a few classes that extend from another class. Inside of the class that is being extended, I have to call some unique code based on which class is calling it.

This is something of an in-depth project, so I created a testcase that should explain what I am trying to do:

class parent {
    function traverseTable($table) {
        foreach($table->getElementsByTagName('tr') {
            $rowCnt++;
            $this->uniqueSearch($rowCnt);
        }
    }
}

class child1 extends parent {
    function search($input) {
        //parse input, get $table
        $this->traverseTable($table);
    }

    function uniqueSearch($rowCnt) {
         echo 'child1';
        //Do different things
    }
}

class child2 extends parent {
    function search($input) {
        //parse input, get $table
        $this->traverseTable($table);
    }

    function uniqueSearch($rowCnt) {
         echo 'child2';
        //Do different things
    }
}

Basically, I want to be able to call the uniqueSearch() function from inside the loop in Class Parent; but the above syntax does not seem to work. Anybody have any ideas? The real size of the uniqueSearch functions vary from 20-100 lines at this point, but might get bigger.

So, you want to call uniqueSearch polymorphically.

  1. Your first problem has nothing to do with this, and is that parent is a reserved word:

    Fatal error: Cannot use 'parent' as class name as it is reserved on line 2

    Fix that.

  2. Your next problem is a simple syntax error:

    Parse error: syntax error, unexpected '{' on line 4

    Fix that.

  3. Then, you have the issue that there is no $table, and no getElementsByTagName in your testcase. Also foreach ($table->getElementsByTagName('tr')) is not valid PHP.

    Fix that.

  4. Your testcase doesn't call any functions.

    Fix that.

Result:

<?php
class base {
    function traverseTable($table) {
        foreach ($table as $element) {
            $rowCnt++;
            $this->uniqueSearch($rowCnt);
        }
    }
}

class child1 extends base {
    function search($input) {
        //parse input, get $table
        // dummy for now:
        $table = Array(0,1,2,3);
        $this->traverseTable($table);
    }

    function uniqueSearch($rowCnt) {
         echo 'child1';
        //Do different things
    }
}

class child2 extends base {
    function search($input) {
        //parse input, get $table
        // dummy for now:
        $table = Array(0,1,2,3);
        $this->traverseTable($table);
    }

    function uniqueSearch($rowCnt) {
         echo 'child2';
        //Do different things
    }
}

$c1 = new child1;
$c2 = new child2;

$c1->search("");
$c2->search("");
?>

And now it works just fine:

child1child1child1child1child2child2child2child2

This question had nothing to do with polymorphism whatsoever, as it turns out; just silly syntax errors.

Thanks for playing.

Declare uniqueSearch() inside parent class as abstract method (that implies that class also has to be declared as abstract). That means each child must implement uniqueSearch() method itself, so it definitely is there.

abstract class parent {
    function traverseTable($table) {
        foreach($table->getElementsByTagName('tr') {
            $rowCnt++;
            $this->uniqueSearch($rowCnt);
        }
    }

    abstract function uniqueSearch($rowCnt);
}