public Public: This class, subclasses, and external objects can all call
protected Protected: subclasses of this class, can be executed, external objects cannot call
private Private: can only be executed by this class, subclasses and external objects cannot be called
Three major characteristics of object-oriented programming
1) Closedness
Closeness can also be called information hiding. It is to separate the use and implementation of a class, leaving only limited interfaces (methods) to connect with the outside. For developers who use this class, they only need to know how to use this class, and do not need to care about how this class is implemented. Doing so allows developers to better focus on other things, while also avoiding the inconvenience caused by interdependencies between programs.
2) Inheritance
Inheritance means that a derived class (subclass) automatically inherits the properties and methods in one or more base classes (parent classes), and can override or add new properties and methods. Inheritance simplifies the creation of objects and classes and increases code reproducibility. Inheritance is divided into single inheritance and multiple inheritance. PHP supports single inheritance, that is to say, a subclass has one and only one parent class.
3) Polymorphism
Polymorphism means that different objects of the same class can obtain different results using the same method. This technology is called polymorphism. Polymorphism enhances software flexibility and reusability.
Class definition
A class can contain its own constants, variables (called "properties") and functions (called "methods").
Like many object-oriented languages, PHP also defines classes through the class keyword plus the class name. The format of the class is as follows:
Definition: Birds of a feather flock together, and objects with similar characteristics are grouped into a class. Classes define the same properties and methods that these similar objects have. A class is a description of similar objects, called the definition of the class, which is the blueprint or prototype of the object of that class.
An object of a class is called an instance of the class. To create an instance of a class, the new keyword must be used.
001ZpQGYty6MeYnSNUh25&690
//Define method
Public function run(){
echo "Runningn";
}
Public function dribblr(){
echo "Dribblingn";
}
Public function pass(){
echo "Passingn";
}
}
//Instantiation from class to object
//When a class is instantiated into an object, use the keyword new, followed by new followed by the name of the class and a pair of brackets
$jordan = new NbaPlayer();
//Attribute members in the object can be accessed through the "->" symbol
echo $jordan->name."n";
//Member methods in the object can be accessed through the "->" symbol
$jordan->dribble();
$jordan->run();
?>
Member methods
Functions in a class are called member methods. The only difference between functions and member methods is that functions implement an independent function, while member methods implement a behavior in the class and are part of the class.
Let’s expand the myobject class above and add a member method to it. The code is as follows:
The function of this method is to output the product name, which is passed in through the parameters of the method.
A class is an abstract description, a collection of objects with similar functions. If you want to use the methods and variables in the class, you must first implement them into an entity, that is, an object.
Class constants
Since there are variables, of course there are also constants. A constant is a quantity that does not change and is a constant value. A well-known constant is pi. To define constants use the keyword const such as:
ConstPI=3.14159;
Constructor
PHP 5 allows developers to define a method as a constructor in a class. Classes with a constructor will call this method every time a new object is created, so it is very suitable for doing some initialization work before using the object.
//Constructor, automatically called when the object is instantiated
function __construct($name,$height,$weight,$team){
echo "It is an NbaPlayer constructorn";
$this->name = $name;
//$this is a pseudo variable in PHP, representing the object itself. You can access the properties and methods of the object through $this->
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
}
//Define method
Public function run(){
echo "Runningn";
}
Public function dribblr(){
echo "Dribblingn";
}
Public function pass(){
echo "Passingn";
}
}
//Class to object instantiation
//When a class is instantiated into an object, use the keyword new, followed by new followed by the name of the class and a pair of brackets
$jordan = new NbaPlayer("Jordan","198cm","98kg","Bull");
//Attribute members in the object can be accessed through the "->" symbol
echo $jordan->name."n";
//Member methods in the object can be accessed through the "->" symbol
$jordan->dribble();
$jordan->run();
//Every time you instantiate an object with new, the constructor will be called with the parameter list after the class name
$james = new NbaPlayer("James","203cm","120kg","Heat")
echo $james->name."n";
?>
Destructor
The destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.
Object reference
//Constructor, automatically called when the object is instantiated
function __construct($name,$height,$weight,$team){
echo "It is an NbaPlayer constructorn";
$this->name = $name;
//$this is a pseudo variable in PHP, representing the object itself. You can access the properties and methods of the object through $this->
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
}
//The destructor will be automatically called at the end of program execution
// Destructors are usually used to clean up resources used by the program. For example, if the program uses a printer, the printer resources can be released in the destructor
function __destruct(){
echo "Destroying".$this->name."n";
}
//Define method
Public function run(){
echo "Runningn";
}
Public function dribblr(){
echo "Dribblingn";
}
Public function pass(){
echo "Passingn";
}
}
//Instantiation from class to object
//When a class is instantiated into an object, use the keyword new, followed by new followed by the name of the class and a pair of brackets
$jordan = new NbaPlayer("Jordan","198cm","98kg","Bull");
//Attribute members in the object can be accessed through the "->" symbol
echo $jordan->name."n";
//Member methods in the object can be accessed through the "->" symbol
$jordan->dribble();
$jordan->run();
//Every time you instantiate an object with new, the constructor will be called with the parameter list after the class name
$james = new NbaPlayer("James","203cm","120kg","Heat")
echo $james->name."n";
//The reference of the object is used to access the properties and methods of the object. $james, $james1 and $james2 are all references to the object
//$james and $james1 are two independent references to the object
//$james2 is the shadow of $james, using the same reference of the object. Any assignment to null is equivalent to deleting the same reference
$james1 = $james;
$james2 = &$james
$james = null;
echo "from now on James will not be used.n"
?>