변수 $this는 PHP에서 무엇을 의미합니까?
.$this
PHP에 항상 저장되어 있고, 그 용도가 무엇인지 전혀 모릅니다.개인적으로 써본 적은 없어요.
변수가 되어 있는지 말해줄 수 있나요?$this
PHP ★★★★★★★★★★★★★★★★★★★?
현재 객체를 참조하는 것으로 객체 지향 코드에서 가장 일반적으로 사용됩니다.
- 참고 자료: http://www.php.net/manual/en/language.oop5.basic.php
- 입문 : http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html
예:
<?php
class Person {
public $name;
function __construct( $name ) {
$this->name = $name;
}
};
$jack = new Person('Jack');
echo $jack->name;
이렇게 하면 'Jack' 문자열이 생성된 개체의 속성으로 저장됩니다.
에 $this
PHP의 변수는 다양한 컨텍스트에서 인터프리터에 대해 시도합니다.
print isset($this); //true, $this exists
print gettype($this); //Object, $this is an object
print is_array($this); //false, $this isn't an array
print get_object_vars($this); //true, $this's variables are an array
print is_object($this); //true, $this is still an object
print get_class($this); //YourProject\YourFile\YourClass
print get_parent_class($this); //YourBundle\YourStuff\YourParentClass
print gettype($this->container); //object
print_r($this); //delicious data dump of $this
print $this->yourvariable //access $this variable with ->
그...$this
현재 객체이러한 기능은 클래스 내의 모든 멤버 변수 및 멤버 메서드에 액세스할 수 있으므로 유용합니다.예를 들어 다음과 같습니다.
Class Dog{
public $my_member_variable; //member variable
function normal_method_inside_Dog() { //member method
//Assign data to member variable from inside the member method
$this->my_member_variable = "whatever";
//Get data from member variable from inside the member method.
print $this->my_member_variable;
}
}
$this
는 PHP PHP를 합니다.Object
변수 배열을 포함하는 통역사가 생성한 것입니다.
「 」에 $this
클래스의 에서는 "Normal Class", "Normal Method", "Normal Class "Normal Method", "Normal Method",$this
는 해당 메서드가 속한 오브젝트(클래스)를 반환합니다.
도 있어요.$this
부모 오브젝트가 없는 경우 정의되지 않습니다.
과 php.net의 PHP에 관한 큰 $this
는 컨텍스트에 따라 동작합니다.https://www.php.net/manual/en/language.oop5.basic.php
이 $에 대한 또 다른 정확한 설명이 있습니다.$이것은 주로 클래스의 속성을 참조하기 위해 사용됩니다.
예:
Class A
{
public $myname; //this is a member variable of this class
function callme() {
$myname = 'function variable';
$this->myname = 'Member variable';
echo $myname; //prints function variable
echo $this->myname; //prints member variable
}
}
출력:
function variable
member variable
이는 다른 많은 객체 지향 언어와 마찬가지로 클래스의 인스턴스를 내부에서 참조하는 방법입니다.
PHP 문서에서:
유사 변수 $이것은 오브젝트 컨텍스트 내에서 메서드가 호출될 때 사용할 수 있습니다.$이것은 호출 오브젝트(보통 메서드가 속한 오브젝트이지만 메서드가 세컨더리 오브젝트의 컨텍스트에서 스태틱하게 호출되는 경우에는 다른 오브젝트)에 대한 참조입니다.
$this를 사용하지 않고 다음 코드 스니펫과 같은 이름의 인스턴스 변수와 생성자 인수를 가지려고 하면 어떻게 되는지 봅시다.
<?php
class Student {
public $name;
function __construct( $name ) {
$name = $name;
}
};
$tom = new Student('Tom');
echo $tom->name;
?>
메아리만 울린다
<?php
class Student {
public $name;
function __construct( $name ) {
$this->name = $name; // Using 'this' to access the student's name
}
};
$tom = new Student('Tom');
echo $tom->name;
?>
이것은 '톰'을 울린다
이것은 긴 상세 설명입니다.이것이 초보자에게 도움이 되기를 바랍니다.아주 간단하게 할게요.
먼저 클래스를 만듭니다.
<?php
class Class1
{
}
tag php는 할 수 .?>
.
에는 '과 ''을 더해서 '성질'을 같이 써볼까요?Class1
.
<?php
class Class1
{
public $property1 = "I am property 1";
public $property2 = "I am property 2";
public function Method1()
{
return "I am Method 1";
}
}
속성은 단순한 변수이지만 클래스 내에 있기 때문에 속성이라는 이름을 붙입니다.
메서드는 단순한 함수이지만 클래스 내에도 있기 때문에 메서드라고 부릅니다.
public
키워드란 스크립트 내의 임의의 장소에서 메서드 또는 속성에 액세스할 수 있음을 의미합니다.
그럼 이 안에 있는 을 어떻게 할 수 요?Class1
정답은 인스턴스 또는 개체를 만드는 것입니다. 개체를 클래스의 복사본으로 생각하면 됩니다.
<?php
class Class1
{
public $property1 = "I am property 1";
public $property2 = "I am property 2";
public function Method1()
{
return "I am Method 1";
}
}
$object1 = new Class1;
var_dump($object1);
오브젝트를 작성했습니다.$object1
의 복사본입니다.Class1
모든 내용을 담고 있습니다.그리고 저희가 다 버렸어요.$object1
를 사용합니다.var_dump()
.
이것으로 알 수 있습니다.
object(Class1)#1 (2) { ["property1"]=> string(15) "I am property 1" ["property2"]=> string(15) "I am property 2" }
래래든 the의 모든 Class1
있다$object1
, 제외)Method1
오브젝트를 덤프할 때 메서드가 표시되지 않는 이유를 알 수 없습니다.
, 그럼 제, 리, 면, 면, 면, 면에 요?$property1
요, , 하다를 해요.var_dump($object1->property1);
.->property1
걸걸가가가가가가
할 수도 요.Method1()
.var_dump($object1->Method1());
.
, 그럼 제가 제, 고, 고에 접속하고 싶다고 해 보겠습니다.$property1
안에서Method1()
, 제가 할께요.
<?php
class Class1
{
public $property1 = "I am property 1";
public $property2 = "I am property 2";
public function Method1()
{
$object2 = new Class1;
return $object2->property1;
}
}
$object1 = new Class1;
var_dump($object1->Method1());
we created 작성했습니다$object2 = new Class1;
which is a new copy of 새로운 카피입니다.Class1
예를 들어보세요.예를 들어보자. Then we pointed to 그리고 나서 우리는property1
부에서$object2
return $object2->property1;
This will print 이것은 인쇄됩니다.string(15) "I am property 1"
브라우저에서브라우저에 표시됩니다.
Now instead of doing this inside 이제 이걸 안에서 하는 대신Method1()
$object2 = new Class1;
return $object2->property1;
우리는 이것을 한다.
return $this->property1;
그 the$this
클래스 안에 개체는 클래스 자체입니다. 개체 래 에 체 조 래 해 용 사 하 the object다는 to위 is니 itself됩참 inside refer class자기를 used클 the내
새 개체를 만든 다음 이렇게 반환하는 대체 수단입니다.
$object2 = new Class1;
return $object2->property1;
또 다른 예
<?php
class Class1
{
public $property1 = 119;
public $property2 = 666;
public $result;
public function Method1()
{
$this->result = $this->property1 + $this->property2;
return $this->result;
}
}
$object1 = new Class1;
var_dump($object1->Method1());
우리는 두 가지 속성을 만들고, 수 함 를 하 성 했 습 력 니 든개 we created을 하 andgers더그aining inte in them we들를것과다고ties$this->result
....
잊지 마세요
$this->property1
=$property1
=119
그들은 같은 가치를 가지고 있다.기타
그게 그 생각을 설명해 주길 바라.
이 비디오 시리즈는 OOP에서 많은 도움이 될 것입니다.
https://www.youtube.com/playlist?list=PLe30vg_FG4OSEHH6bRF8FrA7wmoAMUZLv
클래스를 만들 때 인스턴스 변수와 메서드(함수라고도 함)가 있습니다.$인스턴스 변수에 액세스하면 함수는 이러한 변수를 사용하여 원하는 모든 작업을 수행할 수 있습니다.
다른 버전의 Meder 예:
class Person {
protected $name; //can't be accessed from outside the class
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
// this line creates an instance of the class Person setting "Jack" as $name.
// __construct() gets executed when you declare it within the class.
$jack = new Person("Jack");
echo $jack->getName();
Output:
Jack
$this
는, 발신측 오브젝트(통상은 메서드가 속하는 오브젝트이지만, 세컨더리 오브젝트의 컨텍스트에서 메서드가 스태틱하게 호출되고 있는 경우는 다른 오브젝트)에 대한 참조입니다.
$이것은 특수한 변수이며, 같은 오브젝트(즉,)를 참조합니다.그 자체입니다.
실제로 현재 클래스의 인스턴스를 참조하고 있습니다.
여기 위의 진술이 명확해지는 예가 있다.
<?php
class Books {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ." <br/>";
}
}
?>
이것은 메더가 말한 것처럼 현재 클래스의 예를 나타냅니다.
PHP 문서를 참조하십시오.첫 번째 예시에서 설명하겠습니다.
일반적으로 이 키워드는 클래스 내에서 사용되며, 일반적으로 멤버함수에서는 현재 오브젝트의 클래스(변수 또는 함수)의 비스타틱멤버에 액세스하기 위해서 사용됩니다.
- 이 키워드 앞에는 $ 기호를 붙여야 합니다.
- 이 연산자의 경우 -> 기호를 사용합니다.
- 반면 $이것은 특정 인스턴스의 멤버 변수와 함수를 참조합니다.
예를 들어 $this의 사용법을 알아보겠습니다.
<?php
class Hero {
// first name of hero
private $name;
// public function to set value for name (setter method)
public function setName($name) {
$this->name = $name;
}
// public function to get value of name (getter method)
public function getName() {
return $this->name;
}
}
// creating class object
$stark = new Hero();
// calling the public function to set fname
$stark->setName("IRON MAN");
// getting the value of the name variable
echo "I Am " . $stark->getName();
?>
출력: I am Iron Man
메모: 정적 변수는 글로벌 변수로 기능하며 클래스의 모든 개체 간에 공유됩니다.비정적 변수는 생성된 인스턴스 개체에 고유합니다.
언급URL : https://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php
'source' 카테고리의 다른 글
Java Array List - 두 목록이 동일한지, 순서는 중요하지 않은지 어떻게 알 수 있습니까? (0) | 2022.11.05 |
---|---|
href 식의 역할은 무엇입니까? (0) | 2022.11.05 |
MariaDB/MySQL 선택 쿼리 ID의 json 배열을 연결된 값으로 바꿉니다. (0) | 2022.11.05 |
MySQL ENUM 유형 vs 조인 테이블 (0) | 2022.11.04 |
jQuery는 같은 클래스의 요소를 루프합니다. (0) | 2022.11.04 |