Lecture
Inheritance is not just creating an exact copy of a class, but an extension of an already existing class, so that the descendant can perform some new, characteristic functions only.
So, let us have some class A with certain properties and methods. But what this class does is not entirely satisfactory for us — for example, let it perform most of the functions that are essentially necessary for us, but it does not implement some others. Let us set ourselves the goal of creating a new class B , as if “expanding” the possibilities of class A , adding to it several new properties and methods. This can be done in two fundamentally different ways. The first looks something like this:
class A {
function TestA() { ... }
function Test() { ... }
}
class B {
var $a; // объект класса A
function B(параметры_для_A, другие_параметры){
$a = new A(параметры_для_A);
// инициализируем другие поля B
}
function TestB() { ... }
function Test() { ... }
}
?>
Let us explain: in this implementation, an object of class B contains in its composition a subobject of class A as a property. This property is only a “particle” of an object of class B , nothing more. A subobject does not “know” that it is not really independent, but is contained in class B , therefore it cannot take any actions specific to this class.
We wanted to extend the capabilities of class A , not something that contains objects A. What does "extension" mean? Only one thing: we would like that wherever work with objects of class A is permissible, work with objects of class B is also permissible. But in the example above, this is not at all the case.
So, we have some problems:
1 . We do not see explicitly that class B only expands the possibilities of A , and is not a separate entity;
2 We have to refer to the “part A” of class B through $ obj-> a-> TestA () , and to the members of the class B itself as $ obj-> TestB () . The latter can be quite tedious if, as is often the case, in B many methods from A will be used and much less from B. In addition, it makes us constantly remember the internal structure of class B.
Now, in practice, consider what constitutes the inheritance (or empowerment) of classes:
class B extends A {
function B(параметры_для_A, другие_параметры) {
$this->A(параметры_для_A);
// инициализируем другие поля B
}
function TestB() { ... }
function Test() { ... }
}
?>
The extends keyword indicates that the class being created is just an "extension" of class A , and nothing more. That is, B contains the same properties and methods as A , but, in addition to them, some more additional, "their own".
Now "part A" is located directly inside class B and can be easily accessed along with the methods and properties of class B itself . For example, for an object $ obj of class B , the expressions $ obj-> TestA () and $ obj-> TestB () are valid .
So, we see that, indeed, class B is the embodiment of the idea of "extending functionality
class A. "Please note: we can now forget that B inherited from A some properties or methods - from the outside it looks as if class B implements them on its own.
A little about terminology: the parent class A is called base class and the class is a child class B - derivative from a. Sometimes the base class is also called super class , and derivative - subclass .
Consider another PHP example:
php
class Parent {
function parent_funct () { echo "
" ; }
function test () { echo "
" ; }
}
class Child extends Parent {
function child_funct () { echo "
" ; }
function test () { echo "
" ; }
}
$ object = new Parent ;
$ object = new Child ;
$ object -> parent_funct (); // Выводит 'Это родительская функция'
$ object -> child_funct (); // Выводит 'Это дочерняя функция'
$ object -> test (); // Выводит 'Это дочерний класс'
?>
Child class (subclass) Child inherits all methods and properties of the superclass Parent .
Comments
To leave a comment
Running server side scripts using PHP as an example (LAMP)
Terms: Running server side scripts using PHP as an example (LAMP)