在另一个函数中使用一个函数的变量

时间:2013-10-14 11:57:00

标签: php oop

我确信这一定是一个常见的问题,但我找不到解决办法。

我正在尝试为我的网站创建一个简单的面向对象的计算器。

基本上,用户输入地毯的大小以了解清洁成本。到目前为止,我已经计算了平方英尺。平方英尺在一个函数中计算。我试图在名为Rugtype的单独函数中使用$ squarefootage变量。

如何在其他函数中使用$ squarefootage变量。其他课程怎么样?

我的输出包括错误如下:

The total square footage of your 8' 0'' x 10' 0'' rug is .


Fatal error: Method name must be a string in /home/kokorugs/public_html/instant-quote-new.php on line 44

我的相关代码如下:

<?php 
$widthFeet = $_POST["widthFeet"]; 
$widthInches = $_POST["widthInches"];
$lengthFeet = $_POST["lengthFeet"];
$lengthInches = $_POST["lengthInches"];

$rugtype = $_POST["rugtype"];

$enzyme = $_POST["enzyme"];
$sani512 = $_POST["sani512"];
$velodor = $_POST["velodor"];
$q128 = $_POST["q128"];
$preserve = $_POST["preserve"];
$deepclean = $_POST["deepclean"];

$pickup = $_POST["pickup"];

define('BROADLOOM', '1.75');
define('STANDARD_CLEANING', '2.75');
define('SPECIAL_HANDLING', '3.25');
define('DEEP_CLEANING', '5.00');
define('ANTIQUE', '5.00');
define('WOOL_AND_SILK', '5.00');
define('SILK', '10.00');

class Calc {

    public $widthFeet;
    public $widthInches;
    public $lengthFeet;
    public $lengthInches;
    public $squarefootage;
    public $rugtype;

    function getSquareFootage($widthFeet, $widthInches, $lengthFeet, $lengthInches) {

        $squarefootage = ($widthFeet + ($widthInches / 12)) * ($lengthFeet + ($lengthInches / 12));
        echo "The total square footage of your $widthFeet' $widthInches'' x $lengthFeet' $lengthInches'' rug is $sqft.<br><br>";

    }

    function getRugtype($rugtype) {

        $this->$squarefootage($widthFeet, $widthInches, $lengthFeet, $lengthInches);
        $price = $squarefootage * 2.75;
        echo "The cost of cleaning your standard rug type is $price <br><br>.";

    }

}

//Creating object of class
$squarefootage = new Calc();

if ($_POST['submit']){
    $squarefootage->getSquareFootage($widthFeet, $widthInches, $lengthFeet, $lengthInches);
    $squarefootage->getRugtype($rugtype);
}
?>

2 个答案:

答案 0 :(得分:2)

class Calc {

    public $widthFeet;
    public $widthInches;
    public $lengthFeet;
    public $lengthInches;
    public $squarefootage;
    public $rugtype;

    function getSquareFootage($widthFeet, $widthInches, $lengthFeet, $lengthInches) {

        return ($widthFeet + ($widthInches / 12)) * ($lengthFeet + ($lengthInches / 12));
    }

    function getRugtype($rugtype, $squarefootage) {
        return $squarefootage * 2.75;
    }

}

//Creating object of class
$squarefootage = new Calc();

if ($_POST['submit']){
    $size = $squarefootage->getSquareFootage($widthFeet, $widthInches, $lengthFeet, $lengthInches);
    echo "The total square footage of your $widthFeet' $widthInches'' x $lengthFeet' $lengthInches'' rug is $size.<br><br>";
    $price = $squarefootage->getRugtype($rugtype, $size);
    echo "The cost of cleaning your standard rug type is $price <br><br>.";

}

OR

class Calc {

    public $widthFeet;
    public $widthInches;
    public $lengthFeet;
    public $lengthInches;
    public $squarefootage;
    public $rugtype;

    function getSquareFootage($widthFeet, $widthInches, $lengthFeet, $lengthInches) {
        $this->widthFeet = $widthFeet;
        $this->widthInches = $widthInches;
        $this->lengthFeet = $lengthFeet;
        $this->lengthInches = $lengthInches;
        $this->squarefootage = ($widthFeet + ($widthInches / 12)) * ($lengthFeet + ($lengthInches / 12));
        return $this->squarefootage;
    }

    function getRugtype($rugtype) {
        return $this->squarefootage * 2.75;
    }

}

//Creating object of class
$squarefootage = new Calc();

if ($_POST['submit']){
    $size = $squarefootage->getSquareFootage($widthFeet, $widthInches, $lengthFeet, $lengthInches);
    echo "The total square footage of your $widthFeet' $widthInches'' x $lengthFeet' $lengthInches'' rug is $size.<br><br>";
    $price = $squarefootage->getRugtype($rugtype);
    echo "The cost of cleaning your standard rug type is $price <br><br>.";

}

OR

class Calc {

    public $widthFeet;
    public $widthInches;
    public $lengthFeet;
    public $lengthInches;
    public $squarefootage;
    public $rugtype;

    function __construct($widthFeet = 0, $widthInches = 0, $lengthFeet = 0, $lengthInches = 0) {
        $this->widthFeet = $widthFeet;
        $this->widthInches = $widthInches;
        $this->lengthFeet = $lengthFeet;
        $this->lengthInches = $lengthInches;
        $this->squarefootage = ($widthFeet + ($widthInches / 12)) * ($lengthFeet + ($lengthInches / 12));
    }

    function getSquareFootage() {
        return $this->squarefootage;
    }

    function getRugtype($rugtype) {
        return $this->squarefootage * 2.75;
    }

}

//Creating object of class
$squarefootage = new Calc($widthFeet, $widthInches, $lengthFeet, $lengthInches);

if ($_POST['submit']){
    $size = $squarefootage->getSquareFootage();
    echo "The total square footage of your $widthFeet' $widthInches'' x $lengthFeet' $lengthInches'' rug is $size.<br><br>";
    $price = $squarefootage->getRugtype($rugtype);
    echo "The cost of cleaning your standard rug type is $price <br><br>.";

}

答案 1 :(得分:1)

您将变量声明为公共成员(可能应该是私有的btw),但是您不使用“$ this-&gt;”使用它们时所以它们不能在函数之外使用。

所以你应该这样做:

$this->squarefootage = ($widthFeet + ($widthInches / 12)) * ($lengthFeet + ($lengthInches / 12));

所以,总结一下:

$this->$squarefootage => this is wrong.
$squarefootage => This is wrong too, it does not refer to a class variable but to a method avriable
$this->squarefootage=> correct form.