Design Patterns
1. What is design mode?
Refers to a mature language of the design norms, through this specification, can achieve a certain specified to achieve the effect.
In PHP, there are only simple design patterns: Singleton mode, Factory mode singleton mode
1. What is a singleton mode?
Singleton mode, only one instance (object), one class can produce only one object.
To implement singleton mode, you must resolve the following issues
1. Determine the ways in which new objects can be generated: The Neo and clone, which must be disabled: privatization of construction methods and cloning methods
2. How can I enter into the internal generation after closing the path of the external generation of the object? Solution: A static method that can be entered inside a class without the need to create an object externally.
3. How to get the object inside? Solution: You can create an object in a static method
Problem: This static method can be called multiple times, resulting in multiple objects
4. Solution: Add a static property to save the generated object, if the object is added again later, you have to determine whether the object exists, the existence is not created.
Effect
Object Judging method: instanceof, determine whether an object belongs to a class
Summary: Single-instance mode to achieve: three private (privatization of construction methods, privatization of cloning methods, privatization of static properties, public-owned static methods) Factory mode
1. What is the factory model?
Like a factory, objects of the class need to be created.
Factory mode to achieve: able to produce objects, can load classes.
PHP is a language that supports both process-oriented and object-oriented languages. Not all object-oriented languages have a class concept, such as JS. (Can be ignored)
Three characteristics of object-oriented
Three main features of object-oriented: encapsulation, inheritance and polymorphic encapsulation
Encapsulation: For binding data (attributes) and operations (methods) to data, to form external data hiding. Only externally available interfaces can be invoked for their use. (Methods and properties of public)
The process of encapsulation is called Abstraction: a class of transactions with the same characteristics is extracted from the common content, and the process of describing the text is formed. Inherited
Inheritance: Children have the wealth (attributes) or work (method) of their fathers
If a class has already defined the methods and properties that can be used, there is a class that wants to expand on the basis of that class. Extension classes can inherit from existing classes and apply existing properties and methods directly, without having to define them on their own.
Advantages of inheritance
1. Easy to extend (subclasses can add their own characteristics on the basis of the parent class)
2. Save code: Improve the reusability of code
Parent class
Class father{
Wealth (attribute) money
Work (method): Makemoney ();
}
Subclass: Inheritance
Class Son extends father{
Do not own work, inherit father's work: Makemoney ()
} polymorphic
Polymorphism: Multiple forms, in the case of inheritance, subclasses must override methods of the parent class (the subclass has a method with the same name as the parent class)
Parent class
Class father{
function Makemoney () {
Planting trees
}
}
Subclass: Inherit from parent class
Class Son extends father{
function Makemoney () {
Selling vegetables
}
}
Simulating Java
Father Father = new Son ();
Father.makemoney (); Selling Vegetables PHP inheritance
Inheritance: Subclasses can use the properties and methods of the parent class. Inheritance syntax
Parent class: Having some properties and methods that can be inherited
Subclass: Want to own properties and methods that don't exist but exist in another class
Keyword: extends
Subclass extends Parent class
Access to inherited properties (in-Class): Both public and protected
Access to inherited properties (out-of-Class): Only the public can
Inheriting content
Inheritance does not speak of static inheritance, and the inheritance in PHP is the object of the subclass object inheriting the parent class. (Static properties and methods still meet inheritance criteria, can be used)
Subclasses inherit all the attributes (non-static) and non-private methods of the parent class (non-static)
Protected
Protected is a keyword that is specifically used for inheritance (access modifier qualifier), a property or method that is decorated with the protected keyword, and can be used directly in subclasses, but cannot be used outside of subclasses. Bidirectional inheritance
Inheriting the default is the subclass inherits the parent class, but its real parent can also use the properties and methods inside the subclass as long as it satisfies (public and protected), so the inheritance is considered bidirectional.
Rewrite
A subclass that has a property or method with the same name as the parent class overrides the parent class's properties or methods.
Property Overrides
Effects of Overrides
Inheriting memory analysis
20150110--Magic Method Magic constant + Object-oriented-02