PHP Learning Notes Object-oriented, PHP learning notes
Public: This class, subclasses, and external objects can all be called
Protected protected: This class of subclasses can be executed, and external objects cannot be called
Private: Can only be executed in this class, and subclasses and external objects are not callable
Three main features of object-oriented programming
1) Sealing
Closed, can also be called information hiding. is to separate the use and implementation of a class, leaving only a limited number of interfaces (methods) associated with the outside. For developers who use this class, just know how this class is used, without worrying about how the class is implemented. This allows developers to focus on other things better, while avoiding the inconvenience of inter-dependencies between programs.
2) Inheritance
Inheritance is that derived classes (subclasses) automatically inherit properties and methods from one or more base classes (the parent class), and can override or add new properties and methods. Inheriting this feature simplifies the creation of objects and classes, which increases the weight of the code. Inheritance of single inheritance and multiple inheritance, PHP supports single inheritance, that is, a subclass has only one parent class.
3) Polymorphism
Polymorphism refers to different objects of the same class, and the same method can be used to obtain different results, a technique known as polymorphism. Polymorphism enhances the flexibility and reusability of software.
Definition of Class
A class can contain constants that belong to itself, variables (called "Attributes"), and functions (known as "methods").
Like many object-oriented languages, PHP defines classes by adding class names to the Class keyword. The format of the class is as follows:
Copy the Code code as follows:
<?php
Class myobject{
......
}
?>
Definition: Like birds of a Feather, the objects with similar characteristics are grouped into a class that defines the same properties and methods that these similar objects have. A class is a description of a similar object, known as the definition of a class, as a blueprint or prototype of that class object.
The object of the class is called an instance of the class. To create an instance of a class, you must use the New keyword.
001zpqgyty6meynsnuh25&690
Copy the Code code as follows:
<?php
The definition of a class begins with the keyword class, which typically capitalizes the first letter of each word
Class nbaplayer{
Public $name = "Jordan"; Defining properties
Public $height = "198cm";
Public $team = "Bull";
Public $playerNumber = "23";
Defining methods
Public Function run () {
echo "running\n";
}
Public Function Dribblr () {
echo "dribbling\n";
}
Public Function Pass () {
echo "passing\n";
}
}
Class-To-object instantiation
Class is instantiated as an object using the keyword New,new immediately after the name of the class and a pair of parentheses
$jordan = new Nbaplayer ();
Property members in an object can be accessed by using the "-a" symbol
echo $jordan->name. " \ n ";
A member method in an object can be accessed by using the "-a" symbol
$jordan->dribble ();
$jordan->run ();
?>
Member Methods
A function in a class is called a member method. The only difference between a function and a member method is that the function implements a separate function, and the member method is a behavior in the implementation class, which is part of the class.
Let's expand the MyObject class above and add a member method to it, with the following code:
Copy the Code code as follows:
<?php
classmyobject{
function Getobjectname ($name) {
echo "Product Name:". $name;
}
}
?>
The function of this method is to export the product name, and the product name is passed through the parameters of the method.
A class is an abstract description of a set of objects that are functionally similar. If you want to use the methods and variables in a class, you first need to implement it specifically to an entity, that is, the object.
Class constants
Since there are variables, of course there will be constants. A constant is a constant value that does not change the amount. A known constant is Pi Pi. Define constants using the keyword const such as:
constpi=3.14159;
constructor function
PHP 5 allows the developer to define a method as a constructor in a class. Classes with constructors Call this method each time a new object is created, making it ideal for doing some initialization work before using the object.
Copy the Code code as follows:
<?php
The definition of a class begins with the keyword class, which typically capitalizes the first letter of each word
Class nbaplayer{
Public $name = "Jordan"; Defining properties
Public $height = "198cm";
Public $team = "Bull";
Public $playerNumber = "23";
constructor, which is called automatically when an object is instantiated
function __construct ($name, $height, $weight, $team) {
echo "It's an nbaplayer constructor\n";
$this->name = $name;
$this is a pseudo-variable inside PHP that represents the object itself. You can access the properties and methods of an object in a $this-> way
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
}
Defining methods
Public Function run () {
echo "running\n";
}
Public Function Dribblr () {
echo "dribbling\n";
}
Public Function Pass () {
echo "passing\n";
}
}
Class-To-object instantiation
Class is instantiated as an object using the keyword New,new immediately after the name of the class and a pair of parentheses
$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull");
Property members in an object can be accessed by using the "-a" symbol
echo $jordan->name. " \ n ";
A member method in an object can be accessed by using the "-a" symbol
$jordan->dribble ();
$jordan->run ();
Each time an object is instantiated with new, the constructor is called with the argument list following the class name
$james = new Nbaplayer ("James", "203cm", "120kg", "Heat")
echo $james->name. " \ n ";
?>
Destructors
Copy CodeThe code is as follows:
<?php
The definition of a class begins with the keyword class, which typically capitalizes the first letter of each word
Class nbaplayer{
Public $name = "Jordan"; Defining properties
Public $height = "198cm";
Public $team = "Bull";
Public $playerNumber = "23";
constructor, which is called automatically when an object is instantiated
function __construct ($name, $height, $weight, $team) {
echo "It's an nbaplayer constructor\n";
$this->name = $name;
$this is a pseudo-variable inside PHP that represents the object itself. You can access the properties and methods of an object in a $this-> way
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
}
destructors, which are automatically invoked at the end of the program execution
Destructors are often 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 ";
}
Defining methods
Public Function run () {
echo "running\n";
}
Public Function Dribblr () {
echo "dribbling\n";
}
Public Function Pass () {
echo "passing\n";
}
}
Class-To-object instantiation
Class is instantiated as an object using the keyword New,new immediately after the name of the class and a pair of parentheses
$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull");
Property members in an object can be accessed by using the "-a" symbol
echo $jordan->name. " \ n ";
A member method in an object can be accessed by using the "-a" symbol
$jordan->dribble ();
$jordan->run ();
Each time an object is instantiated with new, the constructor is called with the argument list following the class name
$james = new Nbaplayer ("James", "203cm", "120kg", "Heat")
echo $james->name. " \ n ";
The invocation of a destructor can be triggered by setting the variable to null
The destructor is triggered when the object is no longer in use
$james = null;
echo "From now on James would not be used.\n"
?>
Destructors are removed when all references to an object are deleted or executed when an object is explicitly destroyed.
References to Objects
Copy the Code code as follows:
<?php
The definition of a class begins with the keyword class, which typically capitalizes the first letter of each word
Class nbaplayer{
Public $name = "Jordan"; Defining properties
Public $height = "198cm";
Public $team = "Bull";
Public $playerNumber = "23";
constructor, which is called automatically when an object is instantiated
function __construct ($name, $height, $weight, $team) {
echo "It's an nbaplayer constructor\n";
$this->name = $name;
$this is a pseudo-variable inside PHP that represents the object itself. You can access the properties and methods of an object in a $this-> way
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
}
destructors, which are automatically invoked at the end of the program execution
Destructors are often 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 ";
}
Defining methods
Public Function run () {
echo "running\n";
}
Public Function Dribblr () {
echo "dribbling\n";
}
Public Function Pass () {
echo "passing\n";
}
}
Class-To-object instantiation
Class is instantiated as an object using the keyword New,new immediately after the name of the class and a pair of parentheses
$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull");
Property members in an object can be accessed by using the "-a" symbol
echo $jordan->name. " \ n ";
A member method in an object can be accessed by using the "-a" symbol
$jordan->dribble ();
$jordan->run ();
Each time an object is instantiated with new, the constructor is called with the argument list following the class name
$james = new Nbaplayer ("James", "203cm", "120kg", "Heat")
echo $james->name. " \ n ";
The object's reference is used to access the object's properties and methods, $james, $james 1 and $james2 are references to the object
$james and $james1 are two independent references to an object
$james 2 is the shadow of $james, using the same reference of the object, any one assignment is null equivalent to the deletion of the same reference
$james 1 = $james;
$james 2 = & $james
$james = null;
echo "From now on James would not be used.\n"
?>
http://www.bkjia.com/PHPjc/908172.html www.bkjia.com true http://www.bkjia.com/PHPjc/908172.html techarticle PHP Learning Notes Object-oriented, PHP learning notes public: This class, subclasses, external objects can be called protected protected: This class of subclasses, can execute, external objects ...