English 中文(简体)
PHP 特性 : 本代码的逻辑错误是什么?
原标题:PHP trait : What is the logical error in this Code
  • 时间:2012-05-22 14:32:28
  •  标签:
  • php
  • traits

在代码片段正常运行期间,我投资了大约4个小时的代码,但没有获得要求的结果。

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() . "
";

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

What is the Logic Error in this Code? Why I am getting following Output:

Rectangle Area : 0
Circle Area : 0
问题回答
$this->$radius = $radius;

应该是

$this->radius = $radius;

$hile $width 相同。

在此您正在尝试由假可变美元和箭头运算符( & gt;)调用一个变量, 然后您应该在变量前端调用$





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签