PHP之摘引
PHP之引用
??? 所谓PHP的引用,就是不同的名字访问同一个变量内容。可用在变量、函数以及对象上,用法就是在它们前面加上&符号。下面来细说下引用的类型及作用:
?
一、引用类型
1.1、变量引用:
两个变量指向同一个内容
<?php $a = "abc"; $b = &$a; echo $a;//这里输出:abc echo $b;//这里输出:abc $b = "done"; echo $a;//这里$a的值变为done, 所以输出done echo $b;//这里输出done ?>
1.2、函数传址引用
function test(&$a) { $a=$a+100; } $b=1; test($b); //这里$b传递给函数的其实是$b的变量内容所处的内存地址,通过在函数里改变$a的值,就可以改变$b的值了 echo $b;//输出101
要注意的是,在这里test(1); 的话就会出错,原因自己去想
1.3、函数引用
function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } $a=test();//$b的值:1 $a=5; $a=test();//$b的值:2 $a=&test();//$b的值:3 $a=5; $a=test();//$b的值:6
解释如下 :
???? $a=test()方式调用函数,只是将函数的值赋给$a而已, 而$a做任何改变都不会影响到函数中的$b, 而通过$a=&test()方式调用函数呢, 他的作用是将return $b中的$b变量的内存地址与$a变量的内存地址 指向了同一个地方, 即产生了相当于这样的效果($a=&b;) 所以改变$a的值, 也同时改变了$b的值, 所以在执行了
$a=&test();
$a=5;
以后,$b的值变为了5
这里是为了方便理解函数的引用返回才使用静态变量,其实函数的引用返回多用在对象中
?
1.4、对象的引用
class testa { var $abc="ABC"; } $b=new testa; $c = &$b; // or $c = $b; echo $b->abc;//这里输出ABC echo $c->abc;//这里输出ABC $b->abc="DEF"; echo $c->abc;//这里输出DEF
以上代码是在PHP5中的运行效果;
在PHP5中,对象的复制 是通过引用来实现的。上例中$b=new a; $c=$b; 其实等效于$b=new a; $c=&$b;
PHP5中默认就是通过引用来调用对象, 但有时你可能想建立一个对象的副本,并希望原来的对象的改变不影响到副本。 为了这样的目的,PHP定义了一个特殊的方法,称为__clone.
二、引用的作用
如果程序比较大,引用同一个对象的变量比较多,并且希望用完该对象后手工清除它,个人建议用 "&" 方式,然后用$var=null的方式清除。其它时候还是用php5的默认方式吧。另外, php5中对于大数组的传递,建议用 "&" 方式, 毕竟节省内存空间使用。
三、取消引用
当你 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。例如:
?? $a = 1;
?? $b =& $a;
?? unset ($a);
?>
不会 unset $b,只是销毁了 $a。
再如global 引用,当用 global $var 声明一个变量时实际上建立了一个到全局变量的引用。也就是说和这样做是相同的:
??? $var =& $GLOBALS["var"];
?>
这意味着,例如,unset $var 不会 unset 全局变量。
$this 的引用:在一个对象的方法中,$this 永远是调用它的对象的引用。
?
PS:
PHP有个好处就是可利用其拥有的自动垃圾回收机制(释放内存)。即不需要在使用完变量后做任何释放内存的处理,PHP会帮你完成。当然,我们可以按自己的意愿调用 unset() 函数来释放内存,但通常不需要这么做。
这个时候,即便是手动调用 unset()。详情可考:http://bugs.php.net/bug.php?id=33595
。
折中的办法是在 unset() 函数中调用对象中的 __destruct() 方法;
?
不过在PHP里,至少有一种情况内存不会得到自动释放,如下面:
如果两个对象之间存在着相互引用的关系,如“父对象-子对象”,对父对象调用 unset()不会释放在子对象中引用父对象的内存(即便父对象被垃圾回收,也不行)。
?
------------------------------------------------
补充:
php中对于地址的指向(类似指针)功能不是由用户自己来实现的,是由Zend核心实现的,php中引用采用的是“写时拷贝”的原理,就是除非发生写操作,指向同一个地址的变量或者对象是不会被拷贝的。
通俗的讲
1:如果有下面的代码
$a="ABC";
$b=$a;
其实此时,$a与$b都是指向同一内存地址,而并不是$a与$b占用不同的内存
2:如果在上面的代码基础上再加上如下代码
$a="EFG";
由于$a与$b所指向的内存的数据要重新写一次了,此时Zend核心会自动判断,自动为$b生产一个$a的数据拷贝,重新申请一块内存进行存储。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
