douhuireng4407 2013-10-14 11:56
浏览 38
已采纳

在另一个函数中使用一个函数的变量[复制]

I am sure this must be a common question but I have not been able to find a solution.

I am trying to create a simple Object Oriented calculator for my website.

Basically, users enter the size of a rug to find out the cost of cleaning. So far, I have calculed the square footage. The square footage is calculated in one function. I am trying to use the $squarefootage variable in a separate function called Rugtype.

How can I use the $squarefootage variable in other functions. What about other classes?

My output including error is below:

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

My relevant code is below:

<?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);
}
?>
</div>
  • 写回答

2条回答 默认 最新

  • doubang4881 2013-10-14 12:02
    关注
    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条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用