1. Static Properties/Methods
① definition
Static property/Method before property/method is added with static modifier
The Normal property/method is dependent on the object and must be instantiated after the object
will appear in memory for access and operation.
The static property/method is not dependent on the object and can be accessed when the class is declared
Therefore, it will only have one copy, and will not increase according to the increase of the object
② calls and accesses
Call this class self::$ static property; Self:: Static method ();
Call the parent class parent::$ static property; Parent:: Static method ();
External Access Class Name:: $ static property; Class Name:: Static method ();
③ Delay Binding
The parent class consists of: a () b (Self::a ()) subclass contains: A ()
When the subclass calls the B () method, the B () method of the parent class is found because it has no B () method on its own
The self in the B () method refers to itself, which is called the A () method in the parent class
and change self to static, the A () method that finds the subclass itself is returned
Summary: Self refers to the current class, while static refers to the caller
2. Class constants
① definition
Constants that function only within the class are called Class constants
Can also be understood as non-modifiable static properties
Add a const adornment before a property, you cannot add a permission modifier
You do not need to use the $ symbol when declaring and using
② Magic constant
PHP comes with a class constant that cannot be set, and its value is changed as the environment changes.
The full path and file name of the __file__ file, commonly used to detect the root directory of a Web site when initializing a script
The current line number in the __line__ file; often used to log error messages at debug time
directory where the __dir__ files are located
__CLASS__ returns the current class name
__method__ returns the current method name
3. Single-Case mode
① effect
Waste of resources to prevent a class from being instantiated repeatedly by multiple developers
and restricting it, only one instance is allowed to exist, called a singleton mode
② syntax
Class Name {
Create a static protection property that holds the instantiated results
Static protected $ property name;
Set the construction method to final and protected from being overridden or instantiated directly after inheritance
Final protected function __construct () {
}
Creates a static public method that is used to call directly through the class outside
Static Public Function Method name () {
Determine if an instance already exists
if (self::$ property name instanceof self) {
Return directly if it exists
return self::$ property name;
}else{
If it does not exist, it is instantiated and then returned
self::$ Property name = new self ();
return self::$ property name;
}
}
}
$ object = Class Name:: Method Name ();//Generate a unique object by invoking a method in the class
③ annotations
A final decorated class cannot be inherited; A final decorated method can be inherited, but cannot be overridden
The Instanceof object class name can be used to determine whether an object is an instance of the class
4. Abstract class
① definition
The abstract is added before the class, and the method is abstract if it is pre-modified.
Abstract classes cannot be instantiated; Abstract methods cannot have method bodies, that is, {}
Used to unify the functionality of all subclasses, standardize operations, enhance extensibility
② syntax
Abstract class Parent Class {
Public abstract Function Method name ();
}
③ Note
Classes with abstract methods (including inherited) must be abstract classes; Abstract classes are not necessarily abstract methods
To instantiate an abstract class, you need to inherit and complete all the abstract methods
5. Interface
① definition
is a variant of an abstract class
If a class is an abstraction of an object, then an interface is an abstraction of a class
Interfaces are grouped into different classes, while classes instantiate different objects
② syntax
Interface interface Name {
Public Function method ();
}
Class name implements interface 1, interface 2,... {
Public Function method () {
}
}
③ Note
Because the method in the interface must be abstract, you do not need to add an abstract
An interface can inherit from another interface, the same as the inheritance of a class
④ the difference between an abstract class and an interface
An abstract class can have normal properties/methods/constructs, and only abstract methods must be used in an interface
Methods in an abstract class can be private, and methods in an interface are only public
A class can only inherit from an abstract class, but a class may implement multiple interfaces
@zhnoah
Source: http://www.cnblogs.com/zhnoah/
This article is owned by me and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original
The right to pursue legal liability.
PHP Learning Note--11. Object-Oriented 2