In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "PHP object basic case analysis". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this "PHP object basic case analysis" article can help you solve the problem.
Object-oriented foundation
Difference between process-oriented and object-oriented
Object-oriented keyword
Class: class, which defines the outermost structure of the object-oriented subject, which is used to wrap the data and functions of the subject (functions)
Object: object, the specific representative of a certain type of transaction, also known as an instance
Instantiation: new, the process by which a class produces an object
Class member: member
Method: method, which is essentially a function created in a class-like structure, called a member method or a member function
Attribute: property, essentially a variable created in a class-like structure, called a member variable
Class constant: const, which is essentially a constant created in a class-like structure
Create object
# output object (People) # 1 (0) {} # 1 means: the object number, which has nothing to do with the class, is the sequence number of the object in the whole script (0): the number of member variables (attributes) {} indicates: specific member variable information (key value pair) class object # output 0Buyerobject (Buyer) # 1 (2) {["money"] = > string (2) "20" ["sex"] = > string (4) "male"}
Note: class constants are not accessed by objects
Access modifier qualifier
A modifier keyword in front of a property or method to control where the property or method is accessed
Public: public, accessible both within and outside the class
Protected: protected, only allowed to access within related classes
Private: private, only allowed to be accessed within defined classes
The property must have an access modifier qualifier, and the method can have no access modifier qualifier. The default is public.
Objects within the class
$this, an object built inside the method, automatically points to the object of the method to be called
$this exists inside the method (for internal use only), so it is equivalent to inside the structure of the class
Can access any member modified by the access modifier qualifier
Private members are accessed through public methods (public methods can be accessed outside the class)
# output object (Article) # 1 (2) {["name:protected"] = > string (1) "a" ["type:private"] = > string (3) "art"}
$this represents the object, while the environment of $this is inside the method within the class, so the $this object is accessed within the class, so all properties and methods are not restricted by the access modifier qualifier.
Construction method
_ _ construct () is a magic method built into the system. The property of this method is that the object is called automatically immediately after the object is instantiated.
The purpose of a constructor is to initialize resources, including object properties and other resources
Once the constructor is defined and has its own parameters, it can only be instantiated correctly by using the new class name (parameter list).
Magic methods can also be called directly by objects, but they are of no practical use
Destructing method
_ _ destruct (), which is automatically called when the object is destroyed to release resources
Object destruction
Object has no variable pointing (variable points to other data)
Object is actively destroyed (unset destroys object variable)
End of script execution (automatically release resources)
The end of PHP script execution releases all resources, so destructing methods are generally less used.
# if the object is not destroyed, php will release resources at the end of the run # end__destruct object passes a value
Definition: assign the variable that holds the object to another variable
In PHP, the value of an object is passed by reference: that is, one object variable is assigned to another variable, and two variables point to the same object address, that is, there is only one object
# output object (Article) # 1 (2) {["name"] = > string (6) "xiaoli" ["sex"] = > string (6) "famale"} object (Article) # 1 (2) {["name"] = > string (6) "xiaoli" ["sex"] = > string (6) "famale"} object (Article) # 1 (2) {["name"] = > string (10) "wangxiaohu" ["sex"] = > string (6) "famale"} object (Article) # 1 (2) {["name"] = > string (10) "wangxiaohu" ["sex"] = > string (6) "famale"} range resolution operator (class constant access)
There are two colons composed of "::", which are specially used for class to implement class member operations, which can enable classes to access class members directly.
The scope resolution operator is used for the class (class name) to access class members
Class name:: class member
Range resolution operators can also be used by objects as classes (not recommended)
$object name:: class member
Class constants can only be accessed by classes
Class constants are fixed, and the properties of objects are different from object to object.
Static member
Definition: a class member modified with the static keyword, indicating that the member belongs to class access
Static member
Static attribute
Static method
Static members are explicitly used to access classes, not objects
Static members are only modified by a static keyword, and they can also be accessed by objects.
Static members can also be qualified with different access modifier qualifiers, with the same effect.
Self keyword
Used inside the class (inside the method) instead of writing the class name
Self, like $this for internal objects, can replace the current class name inside the method.
It can ensure that it is convenient for users to change the class name.
The self keyword replaces the class name, so it needs to be matched with the range parsing operator:
# output object (Article) # 1 (0) {} object (Article) # 2 (0) {} class load
Access to the class must ensure that the class already exists in memory, so you need to load the PHP file where the class resides into memory before reusing the class
There are two types of class loading
Manual loading: introducing files containing classes into memory through include between classes that need to be used
Automatic loading: define the class structure and location in advance, write the code of the incoming class file, and find a way to make the code of the loaded class execute when the system needs the class and the memory does not exist (automatic loading is the code that automatically runs the loaded class).
There are two ways to load automatically
Function _ _ autoload ($classname) {# find the corresponding file path and naming convention, manually load} # Custom Class load function function Custom function ($classname) {# find the corresponding file and naming convention, manually load} # register automatic loading sql_autoload_register ('Custom function name')
Custom function: define the loading implementation of the class yourself, and then register with the automatic loading mechanism through spl_autoload_register (multiple automatic loading can be registered)
Magic function _ _ autoload: automatically called by the system, the class name needs to be passed in, and the class is loaded manually inside the function (this method is not recommended for PHP7 and later)
Automatic loading requires good specification when declaring a class.
Class name is the same as file name: class name .php or class name .class.php
Class files are classified and put away.
Example: manual loading
Article.php
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.