How to abstract a class
1. declaration of class;
2. Variables (member properties);
① format: There must be a modifier in front of the member property of the class, public/protected/private/static;
Modifier $ variable name [= default value];
When declaring a class by default, do not give the initial value first, because all objects that are created later have their own properties;
For example public $name = ' Gaoyuanyuan ';
Note: The member property cannot be an expression with an operator, a variable, a method, or a function call;
② Definition Method:
Public $varA = 100; Ordinary VALUES (4 scalars: integers, floating-point numbers, booleans, strings);
Public $varB = myconstant; Constant
Public $varC = self::classconstant; Static properties
Public $varD = Array (true,false); Array
3. Member methods (functions);
① format: [modifier]function function name (parameter 1, parameter 2,... ... ){
[function body];
return [returned value];
}
② modifier: public/protected/private/static/abstract/final
③ Note: A declared member method must be related to an object and cannot be a meaningless operation
Example: The following is a member method that declares several people, typically declaring a member method below a member property
Public function say () {//person can speak method
Echo ' people are talking '; function body
}
Public Function Run () {//How people can walk
Echo ' people are walking '; function body
}
Declaring a phone class
Class phone{ //Declare a phone class
Declaring 4 phone-related member properties
public $manufacturers; First member property, used to store the appearance of a phone
public $solor; Second member property to set the appearance color of the phone
Public $battery _capacity; Third member property to define the battery capacity of the phone
Public $screen _size; Fourth member property to define the screen size of the phone
The first member method is used to declare that the phone has the function of answering the call
Public Function call () {
Echo ' is on the phone '; //function body, can be the content of the call
}
The second member method is used to declare that the phone has the ability to send messages
Public function message () {
Echo ' is sending messages '; function body, which can be the specific content of sending information
}
The third member method is used to declare that the phone has the ability to take pictures
Public function photo () [
Echo ' is taking pictures '; function body, which can be the whole process of taking pictures
}
}
How to abstract a class