简体   繁体   中英

How to call parent class functions when extending in child class

Below is my sample code given

<?php
Class A {
    function SelectRecord()
    {
        $this->DeleteRecord();
        echo "class A - SelectRecord ";
    }
    function DeleteRecord()
    {
        echo "class A - DeleteRecord ";
    }
}

Class B extends a {
    function SelectRecord()
    {
        Parent::SelectRecord();
        echo "class B - SelectRecord ";
    }
    function DeleteRecord()
    {
        echo "class B - DeleteRecord ";
    }
}

$objB = new B();
$objB->SelectRecord();

Output I get is

class B - DeleteRecord 
class A - SelectRecord 
class B - SelectRecord

How can I call the class A DeleteRecord method in class A itself when extending in Class B. When I tried to call from Class A it calls the Class B DeleteRecord method. When I use self::DeleteRecord. It works fine. But when to $this and Self. Shall I replace $this to Self wherever it comes?

You can specify the exact class for the method that is being called:

    <?php
    Class A {
        function SelectRecord()
        {
            A::DeleteRecord();
            echo "class A - SelectRecord ";
        }
        function DeleteRecord()
        {
            echo "class A - DeleteRecord ";
        }
    }

    Class B extends a {
        function SelectRecord()
        {
            Parent::SelectRecord();
            echo "class B - SelectRecord ";
        }
        function DeleteRecord()
        {
            echo "class B - DeleteRecord ";
        }
    }

    $objB = new B();
    $objB->SelectRecord();

results

class A - DeleteRecord 
class A - SelectRecord 
class B - SelectRecord 

Explanation and further reading:

The mechanism that causes the derived class`s method to be invoked is called virtual method . It is used to facilitate polymorphism . All methods in PHP are virtual by default. You can prevent method from being overrided marking it by keyword 'final'.

As I understand your classes are not trying to solve any practical problem and this is just a language learning exercises. There is a great guide on using inheritance in Joshua Bloch's Effective Java :

Inheritance is appropriate only in circumstances where the subclass really is a subtype of the superclass. In other words, a class B should extend a class A only if an “is-a” relationship exists between the two classes. If you are tempted to have a class B extend a class A, ask yourself the question: Is every B really an A? If you cannot truthfully answer yes to this question, B should not extend A. If the answer is no, it is often the case that B should contain a private instance of A and expose a smaller and simpler API: A is not an essential part of B, merely a detail of its implementation.

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