---
title: "PHP中指向对象的变量是引用变量"
author: "mywebcode"
date: "2014-04-22T23:42:00Z"
tags: []
slug: "php"
draft: false
lastmod: "2014-04-30T09:12:55Z"
id: 2058160
url: "https://mywebcode.iteye.com/blog/2058160"
---

在PHP5中，指向对象的变量是引用变量。在这个变量里面存储的是所指向对象的内存地址。引用变量传值时，传递的是这个对象的指向。而非复制这个对象。这与其它类型赋值有所不同，这一点要注意。

![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)下面举一个例子来说明：

====================================

```
<?php
class A{
	public $name="gilr";
}
class Person{
	private $a;
	function setA($_a){
		$this->a=$_a;
	}
	function getA(){
		return $this->a;
	}
}
$a1=new A();
$p=new Person();
$p->setA($a1);
echo $p->getA()->name;
?>
```

就是这个情况。![吐舌头](http://static.blog.csdn.net/xheditor/xheditor_emot/default/tongue.gif)  
![](http://img.blog.csdn.net/20140422234210843?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGhwNTkyMQ=%3D%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FCenter)