개발! 딱 깔끔하고 센스있게!

PHP 에서 인용으로 되돌아가기 본문

개발 스크랩 메모/PHP

PHP 에서 인용으로 되돌아가기

렉사이 2020. 12. 28. 23:52

Google은 PHP 문서를 시험해 본 적이 있습니다.

Stack Overflow 를 검색했지만 만족스러운 답을 찾지 못했습니다.

나는 책을 한 권 읽고, 작가는 책에서 《귀래》를 인용했으나 그것을 해명한 적이 없다.

작가가 사용하는 코드

function &getSchool() { return $this->school; } 

어떤 사람이 간단한 예로 이 개념을 해석할 수 있을까.

대답

네가 이 과목이 있다고 가정해라.

class Fruit { private $color = "red"; public function getColor() { return $this->color; } public function &getColorByRef() { return $this->color; } } 

유엔 속성과 두 가지 방법이 있습니다.

값에 따라 되돌아가기 (기본 행위) 와 다른 인용으로 되돌아가기.두 사람의 차이는:

  • When using the first method, you can make changes to the returned value and those changes will not be reflected inside the private property of Fruit because you are actually modifying a copy of the property's value.
  • When using the second method, you are in fact getting back an alias for Fruit::$color -- a different name by which you refer to the same variable. So if you do anything with it (including modifying its contents) you are in fact directly performing the same action on the value of the property.

다음은 테스트 코드:

echo "\nTEST RUN 1:\n\n"; $fruit = new Fruit; $color = $fruit->getColor(); echo "Fruit's color is $color\n"; $color = "green"; // does nothing, but bear with me $color = $fruit->getColor(); echo "Fruit's color is $color\n"; echo "\nTEST RUN 2:\n\n"; $fruit = new Fruit; $color = &$fruit->getColorByRef(); // also need to put & here echo "Fruit's color is $color\n"; $color = "green"; // now this changes the actual property of $fruit $color = $fruit->getColor(); echo "Fruit's color is $color\n"; 

행동이 일어나다.

경고: 문헌에 합법적인 용도를 참고할 수밖에 없었지만, 그것은 아주 적게 사용해야 할 특성 중 하나일 뿐만 아니라 당신이 먼저 어떤 대용품을 고려한 후에만 사용할 수 있습니다.

경험이 부족한 프로그래머는 과도한 사용에 인용해 특정 문제를 해결할 수 있다는 것을 인용할 수 있는 단점을 보지 않기 때문이다.

Comments