이 글의 내용은 [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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


핫 AI 도구

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

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

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

Clothoff.io
AI 옷 제거제

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

인기 기사

뜨거운 도구

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구

안전한 시험 브라우저
안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

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

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