Friday, 26 February 2021

OOP child class, parent class, child is also an instance of parent, Abstract class, PHP access modifiers

 Class A{

    function call_child_method(){
        if(method_exists($this, 'child_method')){
            $this->child_method();
        }
    }
}

And the method is defined in the extending class:

Class B extends A{
    function child_method(){
        echo 'I am the child method!';
    }
}

So with the following code:

$test = new B();
$test->call_child_method();

The output will be:

I am a child method!

You in fact are calling a method of class B to call another method of class B 
https://stackoverflow.com/questions/1944827/php-how-to-call-function-of-a-child-class-from-parent-class
----------------------------------------------------------
Class B extends class A, meaning class B is also an instance of Class A, all Class A methods will be availble to be used in class B, so $this->child_method() in A $this is instance of B also an instance of A.

Not a separate instance; the ChildClass is a ParentClass instance, when talking about inheritance.

In words, this is like:

when creating a dog, do we also create an instance of an animal?

We don't create a dog and (separately) create an animal; the dog is the animal instance. And if we create a poodle, the poodle is the dog and is the animal.


https://stackoverflow.com/questions/53395800/does-creating-an-instance-of-a-child-class-create-an-instance-of-the-parent-clas#:~:text='No'%2C%20it%20is%20an,class%2C%20not%20of%20the%20Parent.

In PHP, for same method, method in child will auto override, to use parent method call parent::method()
https://www.geeksforgeeks.org/this-keyword-in-php/


PHP - What are Abstract Classes and Methods?

Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks.

An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.

An abstract class or method is defined with the abstract keyword:

When inheriting from an abstract class, the child class method must be defined with the same name, and the same or a less restricted access modifier. So, if the abstract method is defined as protected, the child class method must be defined as either protected or public, but not private. Also, the type and number of required arguments must be the same. However, the child classes may have optional arguments in addition.


https://www.w3schools.com/php/php_oop_classes_abstract.asp



PHP - Access Modifiers

Properties and methods can have access modifiers which control where they can be accessed.

There are three access modifiers:

  • public - the property or method can be accessed from everywhere. This is default
  • protected - the property or method can be accessed within the class and by classes derived from that class
  • private - the property or method can ONLY be accessed within the class
https://www.w3schools.com/php/php_oop_access_modifiers.asp


No comments:

Post a Comment