Abstract classes in PHP and abstract methods/static properties and static methods/php in simple interest mode (single-state mode)/serialization and crossdress (serialization and deserialization)/constraint Type/Magic method summary

Source: Internet
Author: User

Objective

Oop

Learning PHP for a long time, today to summarize the abstract classes in PHP and abstract methods/static properties and static methods/php in the simple interest mode (single-state mode)/serialization and crossdress (serialization and deserialization).

1 abstract classes and abstract methods in PHP
   
1. What is an abstract method?
Methods that do not have method body {} must be decorated with the abstract keyword. Such a party, we call the abstract method.
abstract function say (); Abstract methods

2. What is abstract class?
Classes that contain abstract methods are called abstract classes. Abstract classes must be decorated with the abstract keyword.
Abstract class person{}

3. Precautions for abstract class
① Abstract classes can contain non-abstract methods;
② classes that contain abstract methods must be abstract classes;
Abstract classes, and do not necessarily have to contain abstract methods;
③ abstract class, cannot be instantiated.
(Abstract classes may contain abstract methods, abstract methods have no method body, and instantiation calls have no meaning.) )

We use abstract classes for the purpose! is to limit the instantiation!!!

4. Subclasses inherit abstract classes, then subclasses must override all the abstract methods of the parent class. Unless the subclass is also an abstract class.

5. What is the use of abstract classes?
① restrict instantiation. (abstract class is an incomplete class, the abstract method inside does not have a method body, so can not be instantiated)
② Abstract classes provide a specification for the inheritance of subclasses. Subclasses inherit an abstract class, they must contain and implement the abstract methods that are defined in the abstract class.

2 static properties and static methods
  
1. Static
① can modify properties and methods, called static properties, static methods, also called Class properties, class methods;
② static Properties, static methods, can only be called directly using the class name.
Use "Class Name:: $ static Property", "Class Name:: Static method ()"
Person:: $sex; Person::say ();
③ static properties, methods, are declared when the class is loaded. Before the object is produced;
④ static methods, non-static properties or methods cannot be called;
A non-static method that can invoke a static property or method;
(Because static properties and methods are generated when the class is loaded, not static property methods, which are not instantiated at this time)
⑤ in a class, you can use the Self keyword to refer to the class name.
Class person{
Static $sex = "Nan";
function say () {
echo self:: $sex;
}
}
⑥ static properties are shared. That is, new many objects are also shared with one property.

2. Final
①final decorated class, this class is the final class, cannot be inherited;
②final modification method, this method is the final method, cannot be overridden!
③final cannot modify properties.

3, the const keyword;
Declare a constant in a class, you cannot use the Define () function! The CONST keyword must be used.
Like the Define () declaration, the Const keyword declares that a constant cannot take $ and must all be capitalized!
Once a constant is declared, it cannot be changed. When called with static, use the class name Emperor love with Person:: constant.

"Small sum" of several special operators
1,. can only connect strings; "". ""
2. + = Declare array is, association key and value ["Key" = "value"]
3. The object ($this new object) is low. Use member properties, member methods.
4.:: ① Use the parent keyword to invoke the method of the same name in the parents class; Parent::say ();
② uses the class name (and self) to invoke static properties, static methods, and constants in the class.


3 simple interest Mode in PHP (single-state mode)
   
Simple interest mode is also called a single-state mode


You can guarantee that a class can have only one instance of an object.

Key points of implementation:
The ① constructor is privatized and does not allow objects to be created with the New keyword.
② provides a way to obtain an object externally. Determines whether the object is empty in the method, creates an object if it is empty, and returns if it is not empty directly.
The properties of the ③ instance object and the methods of past objects must all be static.
After ④, creating objects only uses the static methods we provide. $s 1 = singleton::getsingle ();

1 classsingleton{2         Private Static $single=NULL;3         Private function__construct () {}4         Static functionGetsingle () {5             if(!self::$single){6Self::$single=NewSelf ();7             }8             returnSelf::$single;9         }Ten         function__destruct () { One             Echo"Ah, I have been destroyed." "; A         } -     } -      the     $s 1= Singleton::Getsingle (); -     $s 2= Singleton::Getsingle (); -     $s 3= Singleton::Getsingle (); -     $s 4= Singleton::getsingle ();
simple code for simple interest mode

4 Serialization and Crossdress serialization (serialized and deserialized)
   
1, serialization: The object through a series of operations, into a string of your process, called serialization;
2. Crossdress: The process of converting a serialized string to an object, called crossdress;
3. When to use serialization?
When the ① object needs to be transmitted over the network;
② objects need to be persisted in a file or database;
4, how to realize the serialization of objects and crossdress?
Serialization: $str = Serialize ($duixiang);
crossdress: $duixiang = unserialize ($STR);
5, __sleep () Magic Method:
① the __sleep () function is automatically executed when the object is serialized;
The ②__sleep () function requires the return of an array, the value in the array, the property that can be serialized, the attribute not in the array, and cannot be serialized;
function __sleep () {
Return Array ("name", "Age");//Only name/age two properties can be serialized
}
6, __wakeup () Magic Method:
① the __wakeup () method is called automatically when crossdress objects are being serialized;
When ② is called automatically, it is used to re-replicate the new object properties generated by crossdress.
function __wakeup () {
$this->name = "John Doe";

5 Constraint types
   
1, type constraint: refers to the variable before, plus the data type, used to constrain the variable can only hold the corresponding data type. (This operation is common in strongly typed languages, where only array and object type constraints are implemented in PHP)
2, if the type constraint is a class, then this class and the subclass object of this class, can pass;

3. In PHP, type constraints can only occur in the formal parameters of a function.
Class person{}
Class Student Extend

function func (person $p) {
Constrain the parameters of the function, only accept the person class and the person subclass
echo "111";
Echo $p->name;
}
Func (New Person ()); √
Func (New Student ()); √
Func ("111"); X

As the form of new person (), we call it "anonymous function";

6 Magic Method Small Summary
   
1, __construct (): constructor, new object, automatically call
2, __destruct (): destructor, automatically called when an object is destroyed
3. __get (): called automatically when accessing private properties in a class. Pass the Read property name, return the $this-> property name
4, __set (): Automatically called when assigning a value to a class's private property. Passing property names and property values that need to be set
5, __isset (): Automatically called when the object private property is detected using Isset (). Pass the detected property name, return Isset ($this property name);
6. __unset (): Automatically called when the object private property is deleted using unset (). Pass the deleted property name, execute unset ($this-property name) in the method;
7. __tostring (): Automatically called when using Echo to print an object. Returns the actual content of the object you want to print, and the return must be a string;
8, __call (): called automatically when a method that is undefined or not exposed in a class is invoked. Pass the called function name, and an array of parameter lists;
9, __clone (): When cloning an object using the Clone keyword, it is called automatically. The function is to initialize the newly cloned object;
10, __sleep (): Automatically called when the object is serialized. Returns an array in which the values in the array are properties that can be serialized.
11, __wakeup (): When the object is deserialized, it is called automatically. To deserialize the newly generated object, initialize the replication;
12, __autoload (): You need to declare a function outside the class. Called automatically when instantiating a class that is a life. Passing the instantiated class name, you can automatically load the corresponding class file using the class name.

Study time notes, there may be some mistakes in the place, welcome to the criticism of the guidance.

Reflection, re-reel, Daily Harvest a little---------------------expect better self

Abstract classes in PHP and abstract methods/static properties and static methods/php in simple interest mode (single-state mode)/serialization and crossdress (serialization and deserialization)/constraint Type/Magic method summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.