블로그 이미지
Don't Stop
angelyr

Recent Post

Recent Comment

Archive

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
  • total
  • today
  • yesterday

<?php

    class A{

        private $att = 'A<br/>';

        function operation(){

            echo "In A : {$this->att}";

        }

    }

 

    class B extends A{

        private $att = 'B<br/>';

        function operation(){

            echo "In B :{$this->att}";

            parent::operation(); // 부모클래스의 operation 함수

        }

    }

    $a = new A();

    $b = new B();

    $a->operation();

    $b->operation();

?>

 

/* 클래스 A의 $att를 public/protected 선언시 실행결과

* In A : A

  In B : B

  In A : B

*

* 클래스 A의 $att를 private 선언시 실행결과

* In A : A

  In B : B

  In A : A 

*  

* 클래스 A의 $att가 public/protected 일때 함수는 부모의 함수를 호출하지만 값은 자식의 값을 나타내었다

* 이 상황에 대하여 여러가지로 실험을 해보았지만 정확한 결론은 내지 못했다.

* 다만 의문이 드는 것을 전처리적으로 생각했을 때 

*

    class B extends A{

            private $att = 'B<br/>';

            function operation(){

                echo "In B :{$this->att}";

                function operation(){

                      echo "In A :{$this->att}"

                }

            }

    }

* 가 되어서 public/protected에서는 A의 변수에도 접근이 가능하기 때문에 B의 변수가 우선적으로 처리된 것 같다. 

* 정확한 답이 아닌 지극히 주관적인 생각이므로 이 결론은 추천하지 않는다. 

* 여전히 의문은 함수 내에서 {$this->att}에서 중괄호를 빼버리면 출력되지 않는 것이다.  

 

posted by angelyr