1. Abstract classes

Methods in interfaces are unimplemented, while methods in classes are implemented. Is there a form that allows some methods in a class not to be implemented?

  • When some methods in an interface are the same implementation method for all implementation classes, only some methods need to use the polymorphic property
  • For example, eating is different for humans and animals, but breathing is the same, there is no need for humans and animals to achieve the function of breathing respectively
  • abstractThe keyword is used to define abstract classes
  • Before the abstract methodabstractThe keyword can indicate that the method is abstract and does not require a concrete implementation {}
  • Abstract classes can contain ordinary methods, with concrete implementations of the methods
  • The key for inheriting an abstract class isextends
  • Subclasses that inherit from abstract classes need to implement abstract methods defined in the abstract class
  • Abstract classes cannot be instantiated. When a subclass inherits an abstract class, all the abstract methods must be defined

Second, the interface

2.1 Object Interfaces

Interfaces define the common behavior of different classes, and then implement different functions in different classes

  • Interface defines an interface, and implements a class that implements an interface
  • Methods in the interface have no concrete implementation, none{}
  • Classes that implement an interface must provide concrete implementations of methods defined in the interface
  • You cannot instantiate an interface, but you can determine whether an object implements an interface.instanceofKeyword Determines whether an object implements an interface$object instanceof interface
  • Interface extends Interface
  • It is a feature of the interface that all methods defined in the interface must be public

2.2 polymorphic

Because interface methods can be implemented in many ways, the specific implementation of the methods defined in the interface is varied, and this property is called polymorphism

You don’t need to know which class the object belongs to, just determine whether the class of the object implements the interface, you can implement the call, the same code to achieve different results

Image point is the same interface, different object implementation, the result is not the same is polymorphic, such as the human object is introduced, get is the human eat apple, the monkey object is introduced, get is the monkey eat banana. The same line of code behaves differently for objects passed in implementations of different interfaces.

Singleton object

Fourth, PHP object-oriented special practice

4.1 Magic methods _toString() and invoke()

  • __toString() This method is called automatically when an object is used as a String(a __toString() method needs to be defined in the class. Call echo $object
  • __invoke() When an object is called as a method, the method is automatically called (an __invoke() method needs to be defined in the class). Call the object (object (object (parameter)

4.2 Magic methods __call() and __callStatic()

__call() method: This method is called automatically when an object accesses a nonexistent method name.

  • Public function __call(name,name,name,argument){}

Note: The access control keyword must be public; You must take two arguments: the name of the method the object accesses (
n a m e ) , the parameters contained in the method ( Name), the parameters contained in the method (
Argument ==> automatically converted to array).

__callStatic() method: This method is called automatically when an object accesses a nonexistent static method name.

  • Public static function __callStatic(name,name,name,argument){}

Note: same as __call(); This method is static.

These two methods are also known as overloading methods

  • Note the distinction between overwrite
  • With these two methods, the invocation of the same method can correspond to different method implementations (static and dynamic invocation of the same method correspond to different method implementations).

# summary

  • Class: class declaration
  • New: class instantiation
  • -Blair: It’s public
  • Protected: Protected member, visible only in the current class or subclass
  • Private: Private member, visible only to the current class
  • Spl_autoload_register (): automatic loader
  • Extends: Extends a class
  • Static: Declares a static member of a class
  • $this: instance reference
  • Self: reference to the class
  • Traits: Class functionality extends horizontally