OOP inheritance
Inheritance is the mechanism for a base class to get one or more classes. A class that inherits from another class is called a subclass of that class . This relationship is usually likened to a parent class and a subclass . The subclass inherits the attributes of the parent class . These attributes are made up of properties and methods. subclasses can add new functionality outside the parent class, so the subclass is the "extension" of the parent class. In PHP, class inheritance is implemented by the extends keyword. A class that inherits from another class becomes a subclass or a derived class, and the class that inherits from the subclass becomes either a parent class or a base class. (PHP only supports single inheritance , PHP does not support method overloading).
classcomputer{Private $_name= "Lenovo"; Public function__construct () {Echo"<meta charset=utf8>"; } Private function__get ($name) { return $this-$name; //todo:implement __get () method. } Public function_run () {Echo"I am the parent class"; }}//inherits the parent class, but the subclass remains emptyclassNotecomputerextendscomputer{}$notecomputer=NewNotecomputer ();//The subclass instantiation object can also run at this timeEcho $notecomputer-_name;$notecomputer->_run ();
Fields and methods and overrides
In some cases, the fields and methods of the parent class are not particularly needed, so you can modify the field and method of the child class by overriding the subclass .
classcomputer{Private $_name= "Lenovo"; Public function__construct () {Echo"<meta charset=utf8>"; } Public function_run () {return"I am the parent class"; }}classNotecomputerextendscomputer{Private $_name= "IBM"; //I don't need the fields and methods of the parent class, you can override the parent class's fields and methods by overriding the method Public function_run () {return"I am a subclass"; }}$notecomputer=NewNotecomputer ();Echo $notecomputer->_run ();
Subclasses call the fields and methods of the parent class
In order to be secure, we generally encapsulate the methods of the parent class so that the external cannot be called and can only be seen by the subclasses that inherit it. At this point, you need to invoke the parent class through a subclass operation.
classcomputer{//The protected keyword can be called only by the class it inherits from protected $_name= "Lenovo"; Public function__construct () {Echo"<meta charset=utf8>"; } protected function_run () {Echo"I am the parent class"; }}classNotecomputerextendscomputer{ Public functionGetName () {return $this-_name; } Public functionGetrun () {return $this-_run (); }}$noteComputer 1=NewNotecomputer ();Echo $noteComputer 1-getName ();Echo $noteComputer 1->getrun ();
Calling a method of a parent class by overriding
Sometimes, we need to invoke the method content of the parent class by overriding the method, this time we must use the syntax: parent class Name:: Method Name ()parent:: method () can call cannot Parent :: The form of a property call
classcomputer{protected $_name= "Lenovo"; protected function_run () {Echo"Lenovo is running"; }}classNotecomputerextendscomputer{ Public $_name= "IBM"; Public function__construct () {Echo"<meta charset=utf8>"; } Public functionGetName () {Echo $this-_name; EchoParent::_run (); EchoComputer::_run (); }}$notecomputer=NewNotecomputer ();$notecomputer->getname ();
Final
The final keyword prevents a class from being inherited , and sometimes it just wants to do a separate class, and not want to be used by another class, so you have to use this keyword, and it is recommended that you add this keyword to a single class.
Final classcomputer{//unable to inherit a class//method that cannot be inherited Final Public functionrun () {}}classNotecomputerextendscomputer{//will error}Final classcomputer{Final Public functionrun () {}}classNotecomputerextendscomputer{ Public function__construct () {Echo"<meta charset=utf8>"; } Public functionrun () {return"I was running."; }}$noteComputer 1=NewNotecomputer ();$noteComputer 1->run ();
Error message:
abstract classes and methods
abstract methods are special, only declared in the parent class, but implemented in subclasses. The abstract method can be declared only in a class that declares abstractions. Rule: 1. Abstract classes cannot be instantiated and can only be inherited by 2. abstract methods must be overridden by the quilt class method
//abstract class can not be instantiated, that is, to create objects//Abstract classes are used to inherit the subclass, to achieve a specification and resource sharing//As long as there is an abstract method, the class must be an abstract class, the class must be preceded by an abstractionAbstract classcomputer{//abstract class allows defining attribute member fields Public $_name= "Lenovo"; //Abstract method in abstraction, cannot have method body//abstract method cannot implement method body contentAbstract Public functionrun (); //can I create a common method in an abstract class ? Public functionrun2 () {Echo"I am the normal method of the parent class.";}}classNotecomputerextendscomputer{ Public function__construct () {Echo"<meta charset=utf8>"; } //Abstract classes in the abstract method, subclasses must override, otherwise will be error//abstract class common method, do not need to rewrite, subclass will inherit directly down Public functionrun () {Echo"I've been fulfilled."; }}$noteComputer=NewNotecomputer ();$noteComputer-run ();$noteCmputer-_name ();
Interface (interface)
A class cannot implement multiple inheritance, but an interface can define a general specification that implements a service, declares the required functions and constants, but does not specify how to implement it, and does not give the details of the implementation because different entities may need to implement the method definition in different ways. The key is to establish a set of general rules that must be implemented, so long as these principles are met to realize this interface. Rule: 1. the entire class is an abstract method (no declaration of Abstract )2. The interface abstraction method must be public 3. member (field) must be a constant
//Create an interface//interface can not be instantiated//interface is to standardize the implementation of its subclasses, in order to achieve a unified purposeInterfacecomputer{//the member field must be a constant ConstNAME = ' Lenovo '; //the methods in the interface are abstract methods, cannot write the method body//and the interface abstract method does not need to write abstract Public function_run ();}//calling interface constant interface:: ConstantEchoComputer::NAME;//the subclass inherits the interface's method is called implements, the interface can implement moreclassNotecomputerImplementscomputer{//The methods of the interface must be implemented Public function_run () {Echo"I implemented the Run method.";}}$noteComputer=NewNotecomputer ();$noteComputer-_run ();//to invoke the shared properties of an interface by using a classNotecomputer::name;
Do you use an abstract class or an interface?
//
PHP Object-oriented Features III