doumao1917 2011-09-08 15:04
浏览 22
已采纳

从子类调用函数[关闭]

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.

  • 写回答

2条回答 默认 最新

  • dq1230123 2011-09-08 15:14
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)