<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php class A { public $name = "Jhon"; } class Person { private $a; function SetA($_a = "Tom") { $this->a = $_a; } function UpdateA($_a = "Tom") { $this->a->name = $_a; } function GetA() { return $this->a; } } $p = new Person(); $a1 = new A(); echo($a1->name . "<br />"); $p->SetA($a1);//调用SetA方法,将对象传入 echo($p->GetA()->name . "<br />");//$p->GetA()返回的是A的对象,->name就是它的属性name //注意:在php3中,指向对象的变量是引用变量。在这个变量里面存储的是指向对象的内存地址。引用变量传值时,传递的是这个对象的指向,而非复制这个对象。 $p->UpdateA(); echo($p->GetA()->name . "<br />"); $p->UpdateA("Mike"); echo($p->GetA()->name . "<br />"); echo($a1->name . "<br />"); ?> </body> </html>
时间: 2024-11-06 07:56:07