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

Php 在另一个函数中使用一个函数中的变量,php,oop,Php,Oop,我相信这一定是一个常见的问题,但我一直未能找到解决办法 我正在尝试为我的网站创建一个简单的面向对象计算器 基本上,用户输入地毯的大小以了解清洁成本。到目前为止,我已经计算了面积。平方英尺由一个函数计算。我试图在一个名为Rugtype的单独函数中使用$squarecentes变量 如何在其他函数中使用$squareCutes变量。其他课程呢 我的输出(包括错误)如下所示: The total square footage of your 8' 0'' x 10' 0'' rug is . Fa

我相信这一定是一个常见的问题,但我一直未能找到解决办法

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

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

如何在其他函数中使用$squareCutes变量。其他课程呢

我的输出(包括错误)如下所示:

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);
}
?>
类计算{
公帑;
公帑(港币);;
公费$lengthfeets;
公费$lenthinches;
公共建筑面积$平方英尺;
公共类型;
函数GetSquareCutes($widthFeet,$widthInches,$lengthFeet,$lengthInches){
返回($widthfeets+($widthfeets/12))*($lengthfeets+($lengthInches/12));
}
函数getRugtype($rugtype,$SquareCutes){
返回$squareceets*2.75;
}
}
//创建类的对象
$squarecentes=新计算();
如果($_POST['submit'])){
$size=$squareCutes->getsquareCutes($widthfeets,$widthfeets,$lengthfeets,$lengthInches);
echo“您的$widthFeet'$widthInches''x$lengthFeet'$lengthInches'地毯的总面积为$size.

”; $price=$squarecentes->getRugtype($rugtype,$size); echo“清洁标准地毯类型的成本为$price

; }

类计算{
公帑;
公帑(港币);;
公费$lengthfeets;
公费$lenthinches;
公共建筑面积$平方英尺;
公共类型;
函数GetSquareCutes($widthFeet,$widthInches,$lengthFeet,$lengthInches){
$this->widthfeets=$widthfeets;
$this->widthsinches=$widthsinches;
$this->lengthfeets=$lengthfeets;
$this->lengthInches=$lengthInches;
$this->squareceets=($widthfeets+($widthfeets/12))*($lengthfeets+($lengthInches/12));
返回$this->squareceets;
}
函数getRugtype($rugtype){
返回$this->squareceets*2.75;
}
}
//创建类的对象
$squarecentes=新计算();
如果($_POST['submit'])){
$size=$squareCutes->getsquareCutes($widthfeets,$widthfeets,$lengthfeets,$lengthInches);
echo“您的$widthFeet'$widthInches''x$lengthFeet'$lengthInches'地毯的总面积为$size.

”; $price=$squarecentes->getRugtype($rugtype); echo“清洁标准地毯类型的成本为$price

; }

类计算{
公帑;
公帑(港币);;
公费$lengthfeets;
公费$lenthinches;
公共建筑面积$平方英尺;
公共类型;
函数构造($widthfeets=0,$widthfeets=0,$lengthfeets=0,$lengthInches=0){
$this->widthfeets=$widthfeets;
$this->widthsinches=$widthsinches;
$this->lengthfeets=$lengthfeets;
$this->lengthInches=$lengthInches;
$this->squareceets=($widthfeets+($widthfeets/12))*($lengthfeets+($lengthInches/12));
}
函数getSquareCutes(){
返回$this->squareceets;
}
函数getRugtype($rugtype){
返回$this->squareceets*2.75;
}
}
//创建类的对象
$squareceets=新计算($widthfeets,$widthfeets,$lengthfeets,$lengthInches);
如果($_POST['submit'])){
$size=$squareCutes->getsquareCutes();
echo“您的$widthFeet'$widthInches''x$lengthFeet'$lengthInches'地毯的总面积为$size.

”; $price=$squarecentes->getRugtype($rugtype); echo“清洁标准地毯类型的成本为$price

; }
您将变量声明为公共成员(顺便说一句,可能是私有成员),但在使用它们时不使用“$this->”。因此,它们不能在函数之外使用

所以你应该这样做:

$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.

您希望
$this->$squareCutes(…)
做什么?将方法中的值返回到调用代码,而不是在方法中回显它们。我尝试了许多不同的方法,如
$price=$squareCutes*2.75但在这种情况下,$平方英尺没有价值,$价格等于2.75。我在想,也许使用$this->$squarecentes可以访问$squarecentes变量。@MarkBaker,我必须道歉,因为这是我第一次尝试OOP PHP。我已经阅读了文档,并在youtube上浏览了一些教程视频,我正在尝试直接加入。我已经使用过程化PHP创建了计算器,它可以工作,但我正在尝试使用它来学习OOP。你的评论是什么意思?当然现在可以了,但我不得不认为代码答案并没有多大帮助。即使它是代码答案,它也会帮助我。也许我不会马上理解所有内容,但我会花一些时间重新阅读文档,也许它会点击。我真的很感激!
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>.";

}
$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.