-4

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);
}
?>
Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158
  • 2
    http://php.net/manual/en/language.oop5.basic.php – deceze Oct 14 '13 at 11:58
  • What do you expect `$this->$squarefootage(...)` to do? – deceze Oct 14 '13 at 11:59
  • Return the values from your methods to the calling code, rather than echoing them inside the methods – Mark Baker Oct 14 '13 at 12:00
  • I was trying many different things like `$price = $squarefootage * 2.75;` but in that case $squarefootage had no value and $price was equal to 2.75. I was thinking that perhaps using $this->$squarefootage would access the $squarefootage variable. – Raphael Rafatpanah Oct 14 '13 at 12:01
  • @MarkBaker, I have to apologize as this is my first attempt at OOP PHP. I have read the documentation and have gone through some tutorial videos on youtube and I am trying to jump right in. I have created the calculator using procedural PHP and it works, but I am trying to use it to learn OOP. What do you mean by your comment? – Raphael Rafatpanah Oct 14 '13 at 12:03
  • Ahh, I understand your comment now. – Raphael Rafatpanah Oct 14 '13 at 12:04

2 Answers2

3
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>.";

}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Granted this will now work, but can't help but think code only answers aren't all that helpful. – naththedeveloper Oct 14 '13 at 12:06
  • Even though it is a code only answer, it will help me. Perhaps I won't understand all of it right away, but I will spend some time re-reading documentation and perhaps it will click. I do appreciate this! – Raphael Rafatpanah Oct 14 '13 at 12:08
1

You declare your variable as public members (should probably be private ones btw), but you do not use "$this->" when using them. So they cannot be used outside functions.

So you should do this:

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

So, to summarize:

$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.
Scalpweb
  • 1,971
  • 1
  • 12
  • 14