In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how php implements the definition and instantiation of classes". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how php implements the definition and instantiation of classes".
In php, you can use the class keyword plus the class name to define a class, the syntax "[modify the keyword of the class] class class name {properties and methods of the class;}"; you can use the new keyword to instantiate the class into an object, and the syntax "variable name = new class name (parameter list)".
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Definition of the php class
In PHP, you can define a class using the class keyword plus the class name, and then wrap the properties and methods that define the class in the class body with curly braces {}. The syntax format of the class is as follows:
[modifier class keywords] class class name {properties and methods of the class;}
Class names are similar to variable names and function names by following the custom naming rules in PHP, which can be legal tags for any non-PHP reserved word. A legal class name begins with a letter or underscore, followed by several letters, numbers, or underscores. If the class name consists of multiple words, it is customary to capitalize the first letter of each word. In addition, the class name had better have a certain meaning, not just a few letters.
The modifier class keyword is an optional parameter that can be omitted. We usually use the following keywords to modify the class:
Abstract: an abstract class or method that, after being decorated as an abstract class, cannot be instantiated, but can be inherited. If at least one method in a class is declared abstract, the class must also be declared abstract. When inheriting an abstract class, the subclass must redefine all abstract methods in the parent class, and the access control for those methods must be the same as in the parent class.
Final: classes decorated with final cannot be inherited, and methods decorated with final cannot be redefined in subclasses.
Note: a class can contain its own constants, variables (called "member properties" or "properties" in the class), and functions (called "member methods" or "methods" in the class).
For example:
Define a car class with properties such as the color and price of the car
Class car {public $color; / / definition property public $price;}
Instantiation of php class
Instantiation of a class is also called creating an object or instantiating an object or instantiating a class.
Instantiating a class into an object is easy, as long as you use the new keyword followed by a method with the same name as the class. Of course, if you don't need to pass parameters to the object when instantiating the object, you can simply use the class name after the new keyword, without the need for parentheses.
The instantiation format of the object is as follows:
Variable name = new class name (list of parameters), or variable name = new class name ()
The parameters are described as follows:
Variable name: through the reference name of an object created by the class, you can access members of the object through this name
New: keyword indicating that a new object is to be created
Class name: represents the type of the new object
Argument list: specifies that the constructor of the class is used to initialize the value of the object. If there is no defined constructor in the class, PHP automatically creates a default constructor without arguments. (we will explain it in more detail later).
Let's give a simple example:
We define a person's class and instantiate it.
Class Preson {/ / defines a Preson class public $name; / / defines the properties of the class (name, sex, age, etc.) public $age;public $gender;// public.} / / new translation is new, meaning to create a new person and assign the new object to $Preson1. This is the instantiation of $Preson1 = new Preson () / / instantiation class $Preson1- > name = "Zhang San"; $Preson1- > age = 22 countries Play1-> gender = "female"; echo $Preson1- > name.''. $Preson1- > age.''. $Preson1- > gender;// if you want to output the second instance, OK directly, just change the variable name $Preson2 = new Preson (); / / instantiation class $Preson2- > name = "Xiaoliang"; $Preson2- > age = 25 $Preson2- > gender = "male"; at this point, I believe you have a better understanding of "how php implements the definition and instantiation of classes", so you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.