This article mainly introduces PHP object-oriented explanation (3). If you need it, you can refer to the object-oriented concept, which is the core of object-oriented technology. In the display world, what we face is objects, such as computers, televisions, and bicycles. In object-oriented programming, an object is a whole composed of information and descriptions of information processing. it is an abstraction of the real world.
Three main features of an object
Object Behavior: you can perform operations on the object. if you turn on the light, turning off the light is behavior.
Object form: how the object responds, colors, dimensions, and shapes are applied.
Object representation: the representation of an object is equivalent to an ID card, which specifically distinguishes between the same behavior and status.
Object-oriented model
Object-oriented concepts:
Oop (object-oriented programming) can make its code simpler and easier to maintain and has stronger reusability.
The code is as follows:
Final public function test (){
For parent classes that do not want subclass, write final in the class name.
The code is as follows:
Final class BaseClass {
The code is as follows:
// Define an interface
Interface ICanEat {
Public function eat ($ food );
}
We can see that there is no specific implementation of the method in the interface, but there must be a method!
Then, the following is: "Humans will eat"
// Specific object, connected to the interface class Human implements ICanEat {public function eat ($ food) {echo "Human eating". $ food .".
";}}$ Obj = new Human (); $ obj-> eat (" shit ");
Ignore the "food" that I give ".
The code is as follows:
Var_dump ($ obj instanceof ICanEat );
Returns a boolean value.
-- More chestnuts
interface ICanPee extends ICanEat{ public function pee();}class Demon implements ICanPee{ public function pee(){ echo "Can demon pee?"; } public function eat($food){ echo "Can demon eat ".$food; }}$ghost=new Demon();$ghost->pee();$ghost->eat("shit");
An interface is essentially a class that can be inherited or inherited. however, to use an inherited interface, all methods of the parent class and "parent class" must be implemented.
-- Summary
/**
* Interface
* 1. Basic concepts and usage of interfaces
* 2. there is no specific implementation of the methods in the interface.
* 3. classes that implement an interface must provide methods defined in the interface
* 4. you cannot use interfaces to create objects. However, you can determine whether an object has implemented an interface.
* 5. interfaces can inherit interfaces (interfaces extends interfaces)
* 6. all methods defined in the interface must be public, which is a feature of the interface.
*/
Aaaaaaaaaaaaaa
Bu xiang xie le ....................
Ming tian yao ge ..............
The above content is a small series of PHP object-oriented explanation (3), I hope you like it.