For you to explain the use of PHP development tools and analysis

Source: Internet
Author: User
Tags reference
For some time has been puzzled by the reference in PHP transfer, and later check the manual and C source program, and repeated testing, roughly the reference pass in memory mode has a certain understanding, then in order to deepen the impression, wrote a summary, should not have a big problem-of course, this is in the PHP4, in the future version may change. When I wrote the summary, I wanted to exercise my English, so I made a good one. But my English is not good, also lazy to translate, anyway at that time want to see oneself understand on the line. Today's whim, suddenly feel quite useful, so here Hong, please correct me. I don't have time to translate the help that you can understand.

<?php
/*

filename:SetGetTest.php

Comment on assignment by value and referrence
Assuming $var is a varialbe, its handle (pointer) is named as *var,
And its content is named as @var

The memory area of @var are referred by *var, if the *var is the same,
Then the memory areas are the same, so *var are just like a pointer.

1. When $var = $var 1
@var copied from @var1, but in the different memory area,
New *var assigned by the system, pointing to the memory area holding @var
*var and *var1 are different

2. When $var =& $var 1,
*var assigned by *VAR1, and the @var are not assigned or copied,
It is absolutely the same as @var1, and in the same memory area
Both *var and *var1 pointing to the memory area, which means they the are.

Passing by Referrence
3.
Function Set1 (& $s) {
$var =& $s;
}
Set1 ($var 1) Results:
*var1 passing to the function, and *s is the same as *VAR1,
Then *var is the same as *s, the ' result is ' that *var is the same as *var1
And all the contents are the same obviously.

4.
Function Set2 (& $s) {
$var = $s;
}
Set2 ($var 1) Results:
*var1 passing to the function, and *s is the same as *VAR1,
But $var = $s executes, from 1. We can @var is the same as @s,
But *var is different to *s, so @var and @s are not in the same area,
While @s and @var1 are sharing the same memory area, also *VAR1 and *s the are.

5.
normal function return:
function get () {return $var 1;}
Assuming the referred by a variable $result.
Then @result is copied from @var1 but *result are not the same as *var1
When $var = Get ();
A variable $result, as I said above, @result is the same as @var1, but *result
is different from *var1, and next $var = $result executes.
As I said in 1., you can find, @var is the same as @result and the same as @var1,
But *var is different from *result and *var1;

While $var =& get () just means:
The *var is the same as *result, so @var and @result are into the same area,
But they are still different from those of $var 1,
Both the memory area of @var1 and *var1,




6.
Returning by Referrence
function &get () {return $var 1;}
There are two ways to get the result

$var = Get (); and $var =& get (); Now I'll tell the difference
I $var = Get ();
The *result is the same as *VAR1 and @result and @var1 are the same.
and then $var = $result executes,
*var is isn't the same as *result, and also different from *VAR1,
But their contents are the same.

I. $var =& get ();
The *result is the same as *VAR1 and @result and @var1 are the same.
And then $var =& $result executes,
This means $var and $result are the same, both of @ and *

*/

The test is the following

function println ($s = "") {
Print "$s <br>\n";
}

Class Getsettest
{
var $var = null;

Function Setbyref (& $arg) {
$this->var =& $arg;
}

Function Passbyref (& $arg) {
$this->var = $arg;
}

function Setbyval ($arg) {
$this->var = $arg;
}

function &getbyref () {
return $this->var;
}

function Getbyval () {
return $this->var;
}
}

$o = new Getsettest;

println ("============ setbyref getbyref =============");
println ("-----------------Before Change----------------");
$in = "before change";
$o->setbyref ($in);
$outByVal = $o->getbyref ();
$outByRef =& $o->getbyref ();
println ("\ $in:". $in);
println ("\ $outByVal:". $outByVal);
println ("\ $outByRef:". $outByRef);
println ("\ $this->var:". $o->var);
println ("-----------------after the Change-----------------");
$in = "after change";
println ("\ $in:". $in);
println ("\ $outByVal:". $outByVal);
println ("\ $outByRef:". $outByRef);
println ("\ $this->var:". $o->var);
println ();

println ("============ setbyref getbyval =============");
println ("-----------------Before Change----------------");
$in = "before change";
$o->setbyref ($in);
$outByVal = $o->getbyval ();
$outByRef =& $o->getbyval ();
println ("\ $in:". $in);
println ("\ $outByVal:". $outByVal);
println ("\ $outByRef:". $outByRef);
println ("\ $this->var:". $o->var);
println ("-----------------after the Change-----------------");
$in = "after change";
println ("\ $in:". $in);
println ("\ $outByVal:". $outByVal);
println ("\ $outByRef:". $outByRef);
println ("\ $this->var:". $o->var);
println ();

println ("============ passbyref getbyval =============");
println ("-----------------Before Change----------------");
$in = "before change";
$o->passbyref ($in);
$outByVal = $o->getbyval ();
$outByRef =& $o->getbyval ();
println ("\ $in:". $in);
println ("\ $outByVal:". $outByVal);
println ("\ $outByRef:". $outByRef);
println ("\ $this->var:". $o->var);
println ("-----------------after the Change-----------------");
$in = "after change";
println ("\ $in:". $in);
println ("\ $outByVal:". $outByVal);
println ("\ $outByRef:". $outByRef);
println ("\ $this->var:". $o->var);
println ();

/*
The following output was edited and added by Me (Night owl), mainly for up-and viewing convenience add here, the more meat agent, to Longnetpro apology
Output results:
============ setbyref getbyref =============
-----------------before Change----------------
$in: Before Change
$outByVal: Before Change
$OUTBYREF: Before Change
$this->var:before Change
-----------------after Change-----------------
$in: After change
$outByVal: Before Change
$OUTBYREF: After change
$this->var:after Change

============ setbyref Getbyval =============
-----------------before Change----------------
$in: Before Change
$outByVal: Before Change
$OUTBYREF: Before Change
$this->var:before Change
-----------------after Change-----------------
$in: After change
$outByVal: Before Change
$OUTBYREF: Before Change
$this->var:after Change

============ passbyref Getbyval =============
-----------------before Change----------------
$in: Before Change
$outByVal: Before Change
$OUTBYREF: Before Change
$this->var:before Change
-----------------after Change-----------------
$in: After change
$outByVal: Before Change
$OUTBYREF: Before Change
$this->var:after Change
*/

?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.