In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to master the class definition class and member attribute method in PHP". In the daily operation, I believe that many people have doubts about how to master the PHP definition class and member attribute method. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to master the class definition class and member attribute method in PHP". Next, please follow the editor to study!
In fact, object-oriented is to abstract the problem to be dealt with as an object, and then solve the actual problem of the object through the properties and behavior of the object. Let's take a look at how classes and objects should be defined in PHP.
Class definition class
In PHP, this class is an abstract concept. We can understand that everything in the world has its own properties and methods, attributes refer to the nature of a thing, such as a basketball, its properties refer to its size, color, weight and so on; its method is its function.
In a word, a class is a collection of variables (properties of a class) and functions (methods of a class) that act on these variables, and attributes and methods are the basis of a class.
In PHP, we can define a class by adding the class name to the class keyword. Its syntax format is as follows:
[modifier class keywords] class class name {properties and methods of the class;}
The naming rules for class names are very similar to those for variable names and function names, following the custom naming rules in PHP.
The class name begins with a letter and or an underscore and consists of letters, numbers, and underscores.
When a class name is made up of multiple words, it is generally customary to capitalize the first letter of each word
It should be noted that the class name is best to have a certain meaning, it is best not to be made up of a few letters.
In the above syntax format, the keywords that modify the class can be omitted, and abstract and final are usually used as keywords to modify the class.
Member attribute
After understanding the syntax format of the class, we should understand the member attributes of the class, what are the member attributes, and we turn the variables declared directly in the class into member attributes, also known as member variables. Multiple variables can be declared in a class, that is, there are multiple member attributes in the class, and each variable stores different attribute information for the class. Its syntax format is as follows:
Access modifier attribute name = attribute value
What we need to note is that when declaring member properties in a class, be sure to use a keyword in front of it. Unlike when declaring variables, variables must be decorated with keywords in the class. Some of these keywords are meaningful, and if you don't need any other meaning to modify them, use the "var" keyword, which has no specific meaning, but if you have a specific meaning, you need to modify it with another keyword.
Next, I'll give you a list of some common modifier keywords:
Public: the declared member attribute can be used inside, subparts, and outside the class without restriction, indicating that it is public.
Protectrd: use it to modify declared member attributes that can be used inside and subparts of a class, but not outside the class.
Pirvate: the declared member attribute can only be used inside the class, not the subparts and externalities of the class.
Examples are as follows:
Define a class called Family in which member attributes are modified with different keywords.
Member method
A member method is a function defined in a class, which implements an independent function. Unlike a function, a member method implements a behavior of the class and is a part of the class. Multiple member methods can be declared in a class, and it is important to note when declaring member methods that access permissions can be controlled by adding an access modifier before the function keyword.
Examples are as follows:
In PHP, there is also a type declaration
With the introduction of type declarations, we can declare types for the formal parameters and return values of member methods in the following format:
[permission modifier] function method name (type parameter 1, type parameter 2,..., type parameter n): return value type {.}
Parameter types can include integer, string, floating point and Boolean. Examples are as follows:
At this point, the study on "how to master the class definition class and member attribute method in PHP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.