개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.06.18 19:35

Yac::__get 메소드 이해를 위한 도움을 요청합니다.

목록
  • 자료구조마법사 오래 전 2025.06.18 19:35 인기
  • 976
    1
저는 Yac::__get 메소드에 대해 공부하고 있습니다. 하지만 이 메소드의 작동 원리를 제대로 이해하지 못하고 있습니다. __get 메소드는 어떠한 역할을 하며, 정확히 언제 호출되는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  오래 전



    Yac::__get 메소드는 PHP의 Magic Method 중 하나로, 객체의 프로퍼티에 접근할 때 호출됩니다.

    예를 들어, 다음과 같은 코드가 있다고 가정해 보겠습니다.

    #hostingforum.kr
    php
    
    class Person {
    
        private $name;
    
    
    
        public function __get($property) {
    
            if ($property == 'name') {
    
                return $this->name;
    
            } else {
    
                throw new Exception("Undefined property: $property");
    
            }
    
        }
    
    }
    
    
    
    $person = new Person();
    
    $person->name = 'John';
    
    
    
    echo $person->name; // John
    
    


    위 코드에서, `$person->name`은 `$person` 객체의 `name` 프로퍼티를 접근하는 것처럼 보이지만, 실제로는 `Person` 클래스의 `__get` 메소드가 호출됩니다.

    `__get` 메소드는 `$person->name`과 같이 프로퍼티에 접근할 때 호출되며, `$person->name`의 값을 반환합니다.

    이러한 메소드는 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 getter 메소드처럼 사용할 수 있습니다.

    예를 들어, 다음과 같은 코드가 있다고 가정해 보겠습니다.

    #hostingforum.kr
    php
    
    class Person {
    
        private $name;
    
    
    
        public function __get($property) {
    
            if ($property == 'name') {
    
                return $this->getName();
    
            } else {
    
                throw new Exception("Undefined property: $property");
    
            }
    
        }
    
    
    
        private function getName() {
    
            return 'John';
    
        }
    
    }
    
    
    
    $person = new Person();
    
    
    
    echo $person->name; // John
    
    


    위 코드에서, `$person->name`은 `$person` 객체의 `name` 프로퍼티를 접근하는 것처럼 보이지만, 실제로는 `Person` 클래스의 `__get` 메소드가 호출됩니다.

    `__get` 메소드는 `$person->name`과 같이 프로퍼티에 접근할 때 호출되며, `$person->getName()`의 값을 반환합니다.

    이러한 메소드는 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 getter 메소드처럼 사용할 수 있습니다.

    `__get` 메소드는 또한 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 읽기 전용으로 만들 수 있습니다.

    예를 들어, 다음과 같은 코드가 있다고 가정해 보겠습니다.

    #hostingforum.kr
    php
    
    class Person {
    
        private $name;
    
    
    
        public function __get($property) {
    
            if ($property == 'name') {
    
                return $this->name;
    
            } else {
    
                throw new Exception("Undefined property: $property");
    
            }
    
        }
    
    }
    
    
    
    $person = new Person();
    
    $person->name = 'John';
    
    
    
    try {
    
        $person->name = 'Jane';
    
    } catch (Exception $e) {
    
        echo $e->getMessage(); // Undefined property: name
    
    }
    
    


    위 코드에서, `$person->name`은 `$person` 객체의 `name` 프로퍼티를 접근하는 것처럼 보이지만, 실제로는 `Person` 클래스의 `__get` 메소드가 호출됩니다.

    `__get` 메소드는 `$person->name`과 같이 프로퍼티에 접근할 때 호출되며, `$person->name`의 값을 반환합니다.

    이러한 메소드는 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 읽기 전용으로 만들 수 있습니다.

    `__get` 메소드는 또한 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 캐싱할 수 있습니다.

    예를 들어, 다음과 같은 코드가 있다고 가정해 보겠습니다.

    #hostingforum.kr
    php
    
    class Person {
    
        private $name;
    
    
    
        public function __get($property) {
    
            if ($property == 'name') {
    
                return $this->getName();
    
            } else {
    
                throw new Exception("Undefined property: $property");
    
            }
    
        }
    
    
    
        private function getName() {
    
            // 캐싱을 위한 메소드
    
            if (!isset($this->nameCache)) {
    
                $this->nameCache = 'John';
    
            }
    
            return $this->nameCache;
    
        }
    
    }
    
    
    
    $person = new Person();
    
    
    
    echo $person->name; // John
    
    echo $person->name; // John
    
    


    위 코드에서, `$person->name`은 `$person` 객체의 `name` 프로퍼티를 접근하는 것처럼 보이지만, 실제로는 `Person` 클래스의 `__get` 메소드가 호출됩니다.

    `__get` 메소드는 `$person->name`과 같이 프로퍼티에 접근할 때 호출되며, `$person->getName()`의 값을 반환합니다.

    이러한 메소드는 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 캐싱할 수 있습니다.

    `__get` 메소드는 또한 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 유효성 검사할 수 있습니다.

    예를 들어, 다음과 같은 코드가 있다고 가정해 보겠습니다.

    #hostingforum.kr
    php
    
    class Person {
    
        private $name;
    
    
    
        public function __get($property) {
    
            if ($property == 'name') {
    
                return $this->getName();
    
            } else {
    
                throw new Exception("Undefined property: $property");
    
            }
    
        }
    
    
    
        private function getName() {
    
            // 유효성 검사를 위한 메소드
    
            if (empty($this->name)) {
    
                throw new Exception("Name is required");
    
            }
    
            return $this->name;
    
        }
    
    }
    
    
    
    $person = new Person();
    
    
    
    try {
    
        $person->name = '';
    
    } catch (Exception $e) {
    
        echo $e->getMessage(); // Name is required
    
    }
    
    


    위 코드에서, `$person->name`은 `$person` 객체의 `name` 프로퍼티를 접근하는 것처럼 보이지만, 실제로는 `Person` 클래스의 `__get` 메소드가 호출됩니다.

    `__get` 메소드는 `$person->name`과 같이 프로퍼티에 접근할 때 호출되며, `$person->getName()`의 값을 반환합니다.

    이러한 메소드는 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 유효성 검사할 수 있습니다.

    `__get` 메소드는 또한 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 변환할 수 있습니다.

    예를 들어, 다음과 같은 코드가 있다고 가정해 보겠습니다.

    #hostingforum.kr
    php
    
    class Person {
    
        private $name;
    
    
    
        public function __get($property) {
    
            if ($property == 'name') {
    
                return $this->getName();
    
            } else {
    
                throw new Exception("Undefined property: $property");
    
            }
    
        }
    
    
    
        private function getName() {
    
            // 변환을 위한 메소드
    
            return strtoupper($this->name);
    
        }
    
    }
    
    
    
    $person = new Person();
    
    $person->name = 'john';
    
    
    
    echo $person->name; // JOHN
    
    


    위 코드에서, `$person->name`은 `$person` 객체의 `name` 프로퍼티를 접근하는 것처럼 보이지만, 실제로는 `Person` 클래스의 `__get` 메소드가 호출됩니다.

    `__get` 메소드는 `$person->name`과 같이 프로퍼티에 접근할 때 호출되며, `$person->getName()`의 값을 반환합니다.

    이러한 메소드는 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 변환할 수 있습니다.

    `__get` 메소드는 또한 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 로깅할 수 있습니다.

    예를 들어, 다음과 같은 코드가 있다고 가정해 보겠습니다.

    #hostingforum.kr
    php
    
    class Person {
    
        private $name;
    
    
    
        public function __get($property) {
    
            if ($property == 'name') {
    
                return $this->getName();
    
            } else {
    
                throw new Exception("Undefined property: $property");
    
            }
    
        }
    
    
    
        private function getName() {
    
            // 로깅을 위한 메소드
    
            echo "Name: $this->namen";
    
            return $this->name;
    
        }
    
    }
    
    
    
    $person = new Person();
    
    $person->name = 'john';
    
    
    
    echo $person->name; // john
    
    


    위 코드에서, `$person->name`은 `$person` 객체의 `name` 프로퍼티를 접근하는 것처럼 보이지만, 실제로는 `Person` 클래스의 `__get` 메소드가 호출됩니다.

    `__get` 메소드는 `$person->name`과 같이 프로퍼티에 접근할 때 호출되며, `$person->getName()`의 값을 반환합니다.

    이러한 메소드는 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 로깅할 수 있습니다.

    `__get` 메소드는 또한 프로퍼티에 접근할 때 호출되기 때문에, 객체의 프로퍼티를 캐시할 수 있습니다.

    예를 들어, 다음과 같은 코드가 있다고 가정해 보겠습니다.

    ```php
    class Person {
    private $name;

    public function __get($property) {
    if ($property ==

    2025-06-18 19:36

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 38,033건 / 70 페이지

검색

게시물 검색