public Public: This class, subclass, and external objects can be called
protected: This class is a subclass, can be executed, but external objects cannot be called
private: only this class can be executed, and neither subclass nor external objects can be called
Three major features of object-oriented programming
1) Closedness
Closeness, also known as information hiding. It is to separate the use and implementation of a class, and only retain limited interfaces (methods) from external connections. For developers who use this class, they just need to know how this class is used, without caring about how this class is implemented. Doing this allows developers to focus on other things better, while also avoiding the inconvenience caused by interdependence between programs.
2) Inheritance
Inheritance means that the derived class (subclass) automatically inherits the properties and methods from one or more base class (parent class), and can override or add new properties and methods. Inheriting this feature simplifies the creation of objects and classes and increases the reproducibility of the code. Inheriting single inheritance and multiple inheritance. PHP supports single inheritance, that is, a subclass has and only one parent class.
3) Polymorphism
Polymorphism refers to different objects of the same class. Different results can be obtained using the same method. This technology is called polymorphism. Polymorphism enhances the flexibility and reusability of the software.
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 by adding class names to class names. The format of the class is as follows:
<?php
Class myobject{
//……
}
?>
Definition: Birds of a feather flock to a class, which defines the same properties and methods that these similar objects have. A class is a description of a similar object, called a definition of a class, and is a blueprint or prototype of an object of that class.
An object of a class is called an instance of a class. To create an instance of a class, the new keyword must be used.
001ZpQGYty6MeYnSNUh25&690
<?php
//The definition of the class starts with the keyword class, and the naming of the class is usually capitalized in the first letter of each word
class NbaPlayer{
public $name = "Jordan"; //Define attributes
public $height = "198cm";
public $team = "Bull";
public $playerNumber = "23";
//Define the method
public function run(){
echo "Running\n";
}
public function dribblr(){
echo "Dribbling\n";
}
public function pass(){
echo "Passing\n";
}
}
//Instantiation of class to object
//Use the keyword new when instantiating a class as an object, followed by the name of the class and a pair of brackets
$jordan = new NbaPlayer();
//The attribute members in the object can be accessed through the "->" symbol
echo $jordan->name."\n";
//The 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 a function and a member method is that a function implements a certain independent function, while a member method implements a behavior in the class and is part of the class.
Let’s expand the above myobject class and add a member method to it. The code is as follows:
<?php
classmyobject{
function getobjectname($name){
echo "The product name is:".$name;
}
}
?>
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 a group of objects with similar functions. If you want to use methods and variables in a class, you must first implement them specifically on an entity, that is, an object.
Class Constants
Since there are variables, of course there will be constants. A constant is a quantity that will not change, and is a constant value. A well-known constant is Pi Pi. Defining constants using keyword const such as:
ConstPI=3.14159;
Constructor
PHP 5 allows developers to define a method as a constructor in a class. Classes with constructors call this method first every time they create a new object, so it's great for doing some initialization before using the object.
<?php
//The definition of the class starts with the keyword class, and the naming of the class is usually capitalized in the first letter of each word
class NbaPlayer{
public $name = "Jordan"; //Define attributes
public $height = "198cm";
public $team = "Bull";
public $playerNumber = "23";
//Constructor, automatically called when the object is instantiated
function __construct($name,$height,$weight,$team){
echo "It is an NbaPlayer constructor\n";
$this->name = $name;
//$this is a pseudo-variable in PHP, representing the object itself. You can access the properties and methods of an object through $this->
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
}
//Define the method
public function run(){
echo "Running\n";
}
public function dribblr(){
echo "Dribbling\n";
}
public function pass(){
echo "Passing\n";
}
}
//Instantiation of class to object
//Use the keyword new when instantiating a class as an object, followed by the name of the class and a pair of brackets
$jordan = new NbaPlayer("Jordan","198cm","98kg","Bull");
//The attribute members in the object can be accessed through the "->" symbol
echo $jordan->name."\n";
//The 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
<?php
//The definition of the class starts with the keyword class, and the naming of the class is usually capitalized in the first letter of each word
class NbaPlayer{
public $name = "Jordan"; //Define attributes
public $height = "198cm";
public $team = "Bull";
public $playerNumber = "23";
//Constructor, automatically called when the object is instantiated
function __construct($name,$height,$weight,$team){
echo "It is an NbaPlayer constructor\n";
$this->name = $name;
//$this is a pseudo-variable in PHP, representing the object itself. You can access the properties and methods of an object through $this->
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
}
//The destructor will be automatically called when the program execution is over
//The destructor is usually used to clean up resources used by the program. For example, if the program uses a printer, you can release the printer resources in the destructor.
function __destruct(){
echo "Destroying".$this->name."\n";
}
//Define the method
public function run(){
echo "Running\n";
}
public function dribblr(){
echo "Dribbling\n";
}
public function pass(){
echo "Passing\n";
}
}
//Instantiation of class to object
//Use the keyword new when instantiating a class as an object, followed by the name of the class and a pair of brackets
$jordan = new NbaPlayer("Jordan","198cm","98kg","Bull");
//The attribute members in the object can be accessed through the "->" symbol
echo $jordan->name."\n";
//The 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";
//By setting the variable to null, the call of the destructor can be triggered
//The destructor will be triggered when the object is no longer in use
$james = null;
echo "from now on James will not be used.\n"
?>
The destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.
References to objects
<?php
//The definition of the class starts with the keyword class, and the naming of the class is usually capitalized in the first letter of each word
class NbaPlayer{
public $name = "Jordan"; //Define attributes
public $height = "198cm";
public $team = "Bull";
public $playerNumber = "23";
//Constructor, automatically called when the object is instantiated
function __construct($name,$height,$weight,$team){
echo "It is an NbaPlayer constructor\n";
$this->name = $name;
//$this is a pseudo-variable in PHP, representing the object itself. You can access the properties and methods of an object through $this->
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
}
//The destructor will be automatically called when the program execution is over
//The destructor is usually used to clean up resources used by the program. For example, if the program uses a printer, you can release the printer resources in the destructor.
function __destruct(){
echo "Destroying".$this->name."\n";
}
//Define the method
public function run(){
echo "Running\n";
}
public function dribblr(){
echo "Dribbling\n";
}
public function pass(){
echo "Passing\n";
}
}
//Instantiation of class to object
//Use the keyword new when instantiating a class as an object, followed by the name of the class and a pair of brackets
$jordan = new NbaPlayer("Jordan","198cm","98kg","Bull");
//The attribute members in the object can be accessed through the "->" symbol
echo $jordan->name."\n";
//The 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 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. Assigning any value 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"
?>