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/03 Report--
This article introduces the relevant knowledge of "what is the use of PHP citation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The reference to php (that is, adding the & symbol before a variable or function, object, etc.) in PHP means that different names access the content of the same variable. Pointers in C language are different from those in C language. Pointers in C language store the contents of variables and the address stored in memory.
1. Reference to a variable
The reference to PHP allows you to point to the same content with two variables
The copy code is as follows:
two。 Function reference passing (addressing call)
I won't say much about the address call. I'll just give the code below.
The copy code is as follows:
It is important to note that if you test (1); here, you will make a mistake, and think for yourself.
Note: do not precede the & symbol with the $b in "test ($b);" above, but in the function "call_user_func_array", to reference passing parameters, you need the & symbol, as shown in the following code:
The copy code is as follows:
3. The reference to the function returns
Look at the code first.
The copy code is as follows:
Here is an explanation:
What you get in this way $a=test (); is not really a function reference return, which is no different from a normal function call. As for the reason: this is the rule of PHP.
PHP specifies that what is obtained through the $a=&test (); method is the return of the function reference.
As for what is reference return (the PHP manual says: reference return is used when you want to use a function to find out which variable the reference should be bound to.) I didn't understand this bullshit for a long time.
Using the above example to explain is
The function is called in the way of $a=test (), only assigning the value of the function to $a, and any change in $a will not affect the $b in the function.
By calling the function through $a=&test (), its function is to point the memory address of the $b variable in return $b to the same place as the memory address of the $a variable.
So changing the value of $an also changes the value of $b, so the
$a=&test ()
$axi5
Later, the value of $b changes to 5
Static variables are used here in order to make people understand that the reference return of the function is mostly used in the object.
Another official example of php is attached:
The copy code is as follows:
This is the way how we use pointer to access variable inside the class.
/ / the output is "HiHowAreYou"
4. Reference to an object
The copy code is as follows:
The code above is the effect of running in PHP5
In PHP5, the assignment of an object is a process of reference. In the above list, $b=new a; $cased roomb; in fact, it is equivalent to $b=new a; $clockwise roomb
The default in PHP5 is to call an object by reference, but sometimes you may want to make a copy of the object and hope that changes to the original object will not affect the copy. For this purpose, PHP5 defines a special method called _ _ clone.
Since PHP 5, new automatically returns references, so using = & here is out of date and generates E_STRICT-level messages.
In php4, the assignment of objects is a copy process
For example: $b=new a, where new a produces an anonymous an object instance, while $b is a copy of the anonymous object. In the same way, $caterpillar is also a copy of the content of $b. So in php4, in order to save memory space, $b=new an is generally changed to the referenced mode, that is, $baked & new a.
Here's another official example:
In php5, you don't need to add anything else to achieve the "object reference" function:
The copy code is as follows:
Output:
The copy code is as follows:
Only Created $bar and printing $bar
My name is "bar" and I live in "foo".
Now $baz is referenced to $bar and printing $bar and $baz
My name is "bar" and I live in "foo".
Now Creating MasterOne and Two and passing $bar to both constructors
Master: MasterOne | foo: my name is "bar" and I live in "foo".
Master: MasterTwo | foo: my name is "bar" and I live in "foo".
Now changing value of $bar and printing $bar and $baz
My name is "baz" and I live in "foo".
My name is "baz" and I live in "foo".
Now printing again MasterOne and Two
Master: MasterOne | foo: my name is "baz" and I live in "foo".
Master: MasterTwo | foo: my name is "baz" and I live in "foo".
Now changing MasterTwo's foo name and printing again MasterOne and Two
Master: MasterOne | foo: my name is "MasterTwo's Foo" and I live in "foo".
Master: MasterTwo | foo: my name is "MasterTwo's Foo" and I live in "foo".
Also printing $bar and $baz
My name is "MasterTwo's Foo" and I live in "foo".
My name is "MasterTwo's Foo" and I live in "foo".
The analysis of the previous example:
The copy code is as follows:
$bar = new foo ('bar')
$M1 = new MasterOne ($bar)
$m2 = new MasterTwo ($bar)
The $bar in instance objects $M1 and $m2 is a reference to instance $bar, not a copy, which is a characteristic of object references in php5, that is to say,
Within 1.$m1 or $m2, any operation on $bar will affect the related value of the external object instance $bar.
two。 Changes to the external object instance $bar also affect the reference-related values of $bar within $M1 and $m2.
In php4, when you use one object instance to face the properties of another object as above, the equivalent code (that is, reference invocation) is similar to the following:
The copy code is as follows:
Class foo {
Var $bar
Function setBar (& $newBar) {
$this- > bar = & newBar
}
}
5. The role of citation
If the program is large, there are many variables referencing the same object, and you want to clear it manually after using the object, you are recommended to use the "&" mode, and then use the $var=null way to clear it. At other times, use the default mode of php5. In addition, for the transfer of large arrays in php5, it is recommended to use the "&" mode, which saves memory space after all.
6. Dereference
When you unset a reference, you just break the binding between the variable name and its contents. This does not mean that the contents of the variable are destroyed. For example:
The copy code is as follows:
Not unset $b, just $a.
7.global reference
When you declare a variable with global $var, you actually create a reference to the global variable. In other words, it is the same as doing this:
This means, for example, that unset $var does not unset global variables.
If you assign a reference to a variable declared as global inside a function, the reference is only visible inside the function. You can avoid this by using the $GLOBALS array.
Example references global variables within a function
The copy code is as follows:
Think of global $var; as an abbreviation for $var = & $GLOBALS ['var'];. Thus assigning other references to $var only changes the reference to the local variable.
8.$this
In an object's method, $this is always a reference to the object that called it.
/ / next, let's have another episode.
The function of pointing to an address (like a pointer) in php is not realized by the user, but by the Zend core. The reference in php adopts the principle of "copy when writing", that is, unless a write operation occurs, variables or objects pointing to the same address will not be copied.
In a popular way
1: if you have the following code
[code]
In fact, at this time, both $an and $b point to the same memory address, but not $an and $b occupy different memory.
2: if you add the following code to the above code
The copy code is as follows:
Since the data of the memory pointed to by $an and $b needs to be rewritten, the Zend core automatically determines that a copy of $a data is automatically produced for $b and re-requests a piece of memory for storage.
Php references (that is, adding & symbols before variables or functions, objects, etc.) is a high-level topic. Beginners should pay attention to it. It is important to understand php references correctly, which has a great impact on performance, and understanding errors can lead to program errors!
Many people misunderstand that references in php are the same as pointers in C, but in fact they are not, and they are very different. Pointers in C language need to be defined using * except that they do not need to be explicitly declared in the process of array transmission. The function of pointing to addresses (similar pointers) in php is not implemented by the user, but by the Zend core. References in php adopt the principle of "copy on write", that is, variables or objects pointing to the same address will not be copied unless a write operation occurs. For example, the following code:
The copy code is as follows:
$a = array ('axiom.
$b = $a
If the program only executes here, $an and $b are the same, but not like C, $an and $b occupy different memory space, but point to the same piece of memory, this is the difference between php and c, and does not need to be written as $baked memory to represent the memory of $b pointing to $a, zend has already implemented the reference for you, and zend will be very smart to help you determine when to deal with it this way and when not to do so.
If you continue to write the following code later, add a function, pass parameters by reference, and print out the array size.
The copy code is as follows:
Function printArray (& $arr) / / reference passing
{
Print (count ($arr))
}
PrintArray ($a)
In the above code, we pass the $an array into the printArray () function by reference, and the zend engine thinks that printArray () may cause a change to $a, and then automatically produces a copy of $a data for $b and re-requests a piece of memory for storage. This is the "copy-on-write" concept mentioned earlier.
If we change the above code to look like this:
The copy code is as follows:
Function printArray ($arr) / / value passing
{
Print (count ($arr))
}
PrintArray ($a)
The above code passes the $a value directly into printArray (), and there is no reference passing, so there is no write-time copy.
You can test the execution efficiency of the above two lines of code, such as adding a loop 1000 times to see how long it takes to run. The result will let you know that incorrect use of references will lead to performance degradation of more than 30%.
This is the end of the content of "what is the use of PHP quotes"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.