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
uniqueSearchpolymorphically.Your first problem has nothing to do with this, and is that
parentis a reserved word:Fix that.
Your next problem is a simple syntax error:
Fix that.
Then, you have the issue that there is no
$table, and nogetElementsByTagNamein your testcase. Alsoforeach ($table->getElementsByTagName('tr'))is not valid PHP.Fix that.
Your testcase doesn’t call any functions.
Fix that.
Result:
And now it works just fine:
This question had nothing to do with polymorphism whatsoever, as it turns out; just silly syntax errors.
Thanks for playing.