Category
is an abstract concept, which is a collection of objects with the same semantic definition (a collective with the same properties and methods). It is not feasible to use specific classes and can only be instantiated. Take a car as an example. The design drawing of the car is the class, and the car is the object. The focus in design is the creation of classes
The concept of classes in real life
Everything is an object. If the specific computer we use is an object, then the computer is a class. You go to the kindergarten to pick up your child. If you say you want to pick up your child, they will definitely not give it to you (unless they know you and know who your child is). You have to name your child. Your child is the specific target, and the child is a person. Class
Class name writing specifications
The first letter of the class name is capitalized. A class is defined in a file and ends with .class.php as the file name
__________________________________________________________
Object
Object handle
Used to distinguish different objects. After the object is created, a storage space is obtained in the memory. The address of the storage space is the identifier or handle of the object
Properties
Variables defined in a class are member attributes, used to describe the data of the static characteristics of the object. Such as a person’s name, gender, first letter in lowercase
Method
A function defined in a class is a member method, which is used to describe the operating behavior of the dynamic characteristics of the object. Method names are not case-sensitive, cannot have duplicate names, and the first letter is lowercase
Object life cycle
After creation, the life cycle begins. When the program ends or the programmer clears the object, it will be destroyed. PHP will automatically destroy the object
Object life cycle
1 After the program execution is completed, PHP runs the recycling mechanism and deletes the objects in the memory
2 Delete all
After the object is referenced, the object is garbage, and the recycling mechanism automatically deletes the garbage
Steps of object generation
1 Create space for objects in content
2 Execute the construction method
3 Return the reference address of the object
Memory distribution of objects:
1 The object reference is placed in the "stack memory" (the stack stores fixed content)
2 Objects are placed in "heap memory" (the heap stores variable content)
3 Static members are placed in the "data area", which are placed when they are loaded for the first time, so that they can be shared by every object in the heap memory
4 Functions and methods are placed in the code area
Object-oriented features
Abstractness
Abstract the common attributes and methods of a class of objects to form a class. This way of thinking is abstract
Encapsulation:
Encapsulate member methods and member attributes into classes, hide attributes and methods, hide method implementation details, and limit access rights to class members through public protected private final static. Data is protected internally and can only be accessed through authorized member methods. Can be operated. Encapsulate members as much as possible
Inheritance extends:
You can make a class inherit and have the member attributes and methods of another existing class. The inherited class is called the parent class or base class, and the inherited class is a subclass. extends keyword implements inheritance relationship
Polymorphism:
The subclass inherits the parent class and achieves polymorphism by overriding the parent class methods
______________________________________________________________________________
Access modifier (scope descriptor)
public
It can be accessed both inside and outside the class or subclasses, which is the most open permission
private
The attributes and methods of the defined class can be accessed inside the class, but cannot be accessed outside the class or subclasses
protected
Define the attributes and methods of a class, which can be accessed inside the class or subclasses, but not accessible outside the class
Module design
Strong cohesion (functions should be completed within the class as much as possible), weak coupling (open as few methods as possible for external calls). Example: The company's sales team receives projects, and the specific work is left to the company's internal programmers, designers, and server managers to complete collaboratively
______________________________________________________________________________
static static properties and static methods
A data object is required to only serve the class, that is, when the class is available internally but not externally. Creating objects is extremely resource-consuming, so when a method has strong publicity, there is no need to regenerate an instance of the class in order to call this method. The defined methods or variables reside in memory when the program is loaded for the first time and are released when the program ends.
Static methods cannot be overridden by non-static methods, and constructors are not allowed to be declared static
static variable:
Member variables declared through static are static variables or class variables. They are public variables of the class. They are generated when used for the first time. There is only one copy for all objects of the class. They belong to the class, not to the object. . Static variables belong to the class rather than to the object. They can be accessed from anywhere through the class. They are global variables of the class and are stored in memory when the class is created. For multiple objects, static data members are only stored in one place, which can save memory. As long as the value of the static data member is updated once, all objects are guaranteed to access the same updated value.
static method:
The method declared with static is a static method or a class method. When executing this method, the object reference will not be passed to the function, so we cannot access non-static members, only static methods or static variables. Only methods related to classes such as self static parent, etc. can be used. It can be executed without generating objects when using it
$this
is a reference to the current object. It generally appears in methods and is used to obtain member attributes of the class or execute member methods of the class
self ::
Reference to this class, used to obtain the stated member attributes or static member methods of the current class self::run()
parent ::
Reference to the parent class, calling methods or properties of the parent class.
_______________________________________________________________
Construction method & Destruction method
Constructor method__construct()
It is automatically executed when creating an object and has no return value. It is used to perform some initialization work of the class, such as the initialization work of object attributes. The constructor method in PHP4 must have the same name as the class. In PHP5, the constructor method is __construct(). The advantage is It is not affected by the class name. If __construct does not exist, PHP will search for a method with the same name as the class and execute it automatically.
You can pass parameters in the constructor to define attributes. When both the parent class and the subclass define constructors, the constructor of the subclass will be executed
Destructor method__destruct():
Method used to be automatically executed when the object is destroyed in memory, without any parameters
_______________________________________________________________
Object final const
//The methods in the class cannot be modified
//Class cannot be inherited
final class souji {
final public $pinpai; //Error reporting
final function congdian() { //Invalid
echo $this->pinpai . "The phone is charged by 10v voltage";
}
public function kaijidonghua() {
echo "<<< No boot animation >>>>";
}
}
class moto extends souji {
public function __construct() {
// $this->pinpai = "Motorola";
$this->congdian();
}
public function kaijidonghua() {
echo "Motorola trademark";
}
}
$li = new moto();
$li->kaijidonghua();
//Define constants
define("WEBNAME", "文豆");
function aa(){
echo WEBNAME."Focus on PHPWEB development";
}
aa();
class caiwu{
const suilv=0.05;
function __construct(){
echo WEBNAME;
}
function kaigonzi($xingming,$gongzi){
$gongzi = $gongzi-$gongzi*self::suilv;
return $xingming."The salary is: ".$gongzi."yuan";
}
}
/*Employee category*/
class yuangong{
private $xingming;//name
private $gongzisu;//Salary number
function __construct($xingming,$gongzisu){
$this->xingming = $xingming;
$this->gongzisu = $gongzisu;
}
function kaizi(){
$caiwu = new caiwu();
return $caiwu->kaigonzi($this->xingming, $this->gongzisu);
}
}
$lisi =
new yuangong("李思", "6000");
echo $lisi ->
- kaizi
();