
`$this` 키워드는 클래스의 객체를 가리키는 키워드입니다. 클래스의 생성자 함수인 `__construct`에서 `$this` 키워드를 사용하여 클래스의 속성을 초기화할 수 있습니다.
예를 들어, `Client` 클래스의 `__construct` 함수에서 `$this->name`과 `$this->email`은 클래스의 객체를 가리키는 `$this` 키워드를 사용하여 클래스의 속성을 초기화하는 것입니다.
#hostingforum.kr
php
class Client {
private $name;
private $email;
public function __construct($name, $email) {
$this->name = $name; // $this는 Client 클래스의 객체를 가리킵니다.
$this->email = $email; // $this는 Client 클래스의 객체를 가리킵니다.
}
}
`$this` 키워드는 클래스의 메서드에서만 사용할 수 있습니다. 클래스의 속성에 접근할 때 `$this` 키워드를 사용하여 클래스의 객체를 가리키는 것입니다.
#hostingforum.kr
php
class Client {
private $name;
private $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function getName() {
return $this->name; // $this는 Client 클래스의 객체를 가리킵니다.
}
}
`$this` 키워드는 클래스의 메서드에서만 사용할 수 있습니다. 클래스의 속성에 접근할 때 `$this` 키워드를 사용하여 클래스의 객체를 가리키는 것입니다.
2025-07-10 23:31