2012-05-22 2 views
0

이 코드에는 약 4 시간을 투자했지만 코드 조각이 정상적으로 실행되는 동안 필요한 결과를 얻지 못했습니다. 코드는 다음과 같습니다 :PHP 특성 :이 코드의 논리적 오류는 무엇입니까?

trait CircleShape{ 
    public function input($radius){ 
     $this->$radius = $radius; 
    } 
} 

trait AngleShape{ 
    public function input($height, $width){ 
     $this->$height = $height; 
     $this->$width = $height; 
    } 
} 

trait GeneralMethod{ 
    public function get($property){ 
     return $this->$property; 
    } 
} 

class Shape{ 
    private $height, $width, $radius; 
    const PI = 3.1415; 

    use GeneralMethod, AngleShape, CircleShape{ 
     AngleShape::input insteadof CircleShape; 
     CircleShape::input as inputCircle; 
    } 
} 

class Circle extends Shape{ 
    public function area(){ 
     return parent::PI * $this->get('radius') * $this->get('radius'); 
    }  
} 

class Rectangle extends Shape{ 

    use GeneralMethod, AngleShape, CircleShape{ 
     AngleShape::input insteadof CircleShape; 
     CircleShape::input as inputCircle; 
    } 
    public function area(){ 
     return $this->get('height') * $this->get('width'); 
    }  
} 

$rect = new Rectangle; 
$rect->input(12, 2); 
Echo "Area: " . $rect->area() . "\n"; 

$cir = new Circle; 
$cir->inputCircle(10); 
Echo "Circle Area : " . $cir->area() . "\n"; 

이 코드의 논리 오류 란 무엇입니까? 내가 출력 다음 무엇입니까 이유 :

Rectangle Area : 0 
Circle Area : 0 
+0

당신이 정의하고 GET을 사용하는 방법 ($의 prop) 기능이 내게 이상한 것처럼 보입니다. 너 왜 그거 해? 속성의 이름 충돌을 피하려면 적어도 해당 함수에서 이름 짓기를 수행해야합니다. 그렇지 않으면 get()을 사용하면 얻을 수있는 이점을 무시할 수 있습니다. – smarr

답변

4
$this->$radius = $radius; 

$height$width

$this->radius = $radius; 

와 동일해야합니다. 여기

+0

와우! 나는 변화와 문제를 몇 초 안에 해결했습니다. 나는 로직과 실수를 모두 이해합니다. 고맙습니다. –

+0

경고를 해제해야합니다. 그것은 여기에 '정의되지 않은 속성'을 생성함으로써 이러한 실수로부터 당신을 보호 할 것입니까? – Nanne

0

당신은 의사 변수 $이 화살표 운영자에 의해 시도 - 당신이 내려해야 하나 개의 변수를 호출 (>) $의 선행 변수

관련 문제