1

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?

svkks
  • 95
  • 2
  • 10
  • Not sure if https://stackoverflow.com/questions/151969/when-to-use-self-over-this answers your problems. – Nigel Ren Apr 27 '20 at 10:39
  • Does this answer your question? [When to use self over $this?](https://stackoverflow.com/questions/151969/when-to-use-self-over-this) – yivi Apr 27 '20 at 10:46
  • Mine is not static function.So how to use Self. My qustion is I have use $this to call. But it called child class function when method name is same. I want parent Class A function to be called. How to do that – svkks Apr 27 '20 at 10:48
  • 1
    Exactly, yours are not static methods. So you do not use self, you use `$this`. If you want to call the parent class, you use `parent::method()`, like you are doing in your question. – yivi Apr 27 '20 at 11:27
  • @yivi When I call from Class A(Parent Class) to delete a record $this->DeleteRecord(), it calls Class B(child class) DeleteRecord function instead Class A. Since I called from Class A, It should call the method in same Class. But it called the derived class function. If method name is same this confusion comes. How to solve this issue. – svkks Apr 27 '20 at 11:48

1 Answers1

0

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.

Alex T
  • 1,296
  • 2
  • 17
  • 25