[PHP 클래스 및 개체] 개체 및 참조
이 글의 내용은 [PHP 클래스 및 객체] 객체와 참조에 관한 것입니다. 특정 참조 값이 있습니다. 이제 도움이 필요한 친구들이 참조할 수 있습니다.
객체 및 참조
PHP 참조는 별칭입니다. 즉, 두 개의 서로 다른 변수 이름이 동일한 내용을 가리킵니다.
PHP 5에서는 객체 변수가 더 이상 전체 객체의 값을 보유하지 않습니다. 실제 개체 콘텐츠에 액세스하려면 식별자를 저장하기만 하면 됩니다. 객체가 매개변수로 전달되거나 결과로 반환되거나 다른 변수에 할당되면 다른 변수는 원래 변수와 참조 관계가 없지만 둘 다 동일한 식별자의 복사본을 저장합니다. 같은 개체.
Example #1 引用和对象<?phpclass A { public $foo = 1; } $a = new A;$b = $a; // $a ,$b都是同一个标识符的拷贝 // ($a) = ($b) = <id>$b->foo = 2;echo $a->foo."\n";$c = new A;$d = &$c; // $c ,$d是引用 // ($c,$d) = <id>$d->foo = 2;echo $c->foo."\n";$e = new A;function foo($obj) { // ($obj) = ($e) = <id> $obj->foo = 2; } foo($e);echo $e->foo."\n";?>
사용자 기여 노트
/* Notes on reference: A reference is not a pointer. However, an object handle IS a pointer. Example: */<?phpclass Foo { private static $used; private $id; public function __construct() { $id = $used++; } public function __clone() { $id = $used++; } }$a = new Foo; // $a is a pointer pointing to Foo object 0$b = $a; // $b is a pointer pointing to Foo object 0, however, $b is a copy of $a$c = &$a; // $c and $a are now references of a pointer pointing to Foo object 0$a = new Foo; // $a and $c are now references of a pointer pointing to Foo object 1, $b is still a pointer pointing to Foo object 0unset($a); // A reference with reference count 1 is automatically converted back to a value. Now $c is a pointer to Foo object 1$a = &$b; // $a and $b are now references of a pointer pointing to Foo object 0$a = NULL; // $a and $b now become a reference to NULL. Foo object 0 can be garbage collected nowunset($b); // $b no longer exists and $a is now NULL$a = clone $c; // $a is now a pointer to Foo object 2, $c remains a pointer to Foo object 1unset($c); // Foo object 1 can be garbage collected now.$c = $a; // $c and $a are pointers pointing to Foo object 2unset($a); // Foo object 2 is still pointed by $c$a = &$c; // Foo object 2 has 1 pointers pointing to it only, that pointer has 2 references: $a and $c;const ABC = TRUE; if(ABC) { $a = NULL; // Foo object 2 can be garbage collected now because $a and $c are now a reference to the same NULL value } else { unset($a); // Foo object 2 is still pointed to $c }
<?php //The two are meant to be the same$a = "Clark Kent"; //a==Clark Kent$b = &$a; //The two will now share the same fate.$b="Superman"; // $a=="Superman" too.echo $a; echo $a="Clark Kent"; // $b=="Clark Kent" too.unset($b); // $b porced from $a$b="Bizarro"; echo $a; // $a=="Clark Kent" still, since $b is a free agent pointer now. //The two are NOT meant to be the same.$c="King"; $d="Pretender to the Throne";echo $c."\n"; // $c=="King"echo $d."\n"; // $d=="Pretender to the Throne"swapByValue($c, $d); echo $c."\n"; // $c=="King"echo $d."\n"; // $d=="Pretender to the Throne"swapByRef($c, $d); echo $c."\n"; // $c=="Pretender to the Throne"echo $d."\n"; // $d=="King"function swapByValue($x, $y) { $temp=$x;$x=$y;$y=$temp; //All this beautiful work will disappear //because it was done on COPIES of pointers. //The originals pointers still point as they did.}function swapByRef(&$x, &$y) { $temp=$x;$x=$y; $y=$temp; //Note the parameter list: now we switched 'em REAL good. } ?>
관련 추천:
위 내용은 [PHP 클래스 및 개체] 개체 및 참조의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제









이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP에서 데이터베이스 작업은 매우 쉽습니다. 이번 장에서는 CRUD(생성, 읽기, 업데이트, 삭제) 작업을 이해하겠습니다.

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu
