Jump to content

Recommended Posts

I'm trying to write a class with getters and setters that converts between centigrade and fahrenheit so that when one value is set, it updates the other value.

 

I'm struggling to bring together the pieces I think I need.

 

I've got this for the setters and getters...

 

	private $tempC = null;
private $tempF = null;

public function getTempC(){
	return $this->tempC;
}

public function setTempC($tempC){
	$this->tempC = $tempC;
}

public function getTempF(){
	return $this->tempF;
}

public function setTempF($tempF){
	$this->tempF = $tempF;
}

 

And this for the rest of the class (var's and functions)...

 

<?php
class Temp {

      var $TempC;
      var $TempF;
      
      function ConvertCF(){  
        $temp = ($TempC / 5) * 9 + 32;
        return $temp;  
      }
      
      function ConvertFC(){  
        $temp = ($TempF - 32) / 9 * 5;
        return $temp;  
      }
      
    } 
?>

 

But I (obvioulsy) need some direction! 

Link to comment
https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/
Share on other sites

Don't use var that is PHP 4 syntax.  Use public or private.  You need to set the vars in the class, something like this, but of course you need to check and make sure the vars are set or forgo the setters and pass the values into the convert functions:

 

      function ConvertCF(){  
        $this->tempF = ($this->tempC / 5) * 9 + 32;
        return $this->tempF;;  
      }
      
      function ConvertFC(){  
        $this->tempC = ($tempF - 32) / 9 * 5;
        return $this->tempC;  
      }

Also, in one code block you show $tempX and in the other you use $TempX. Pick one.

I suggest you only store one temperature. I would recommend Kelvin but that's probably an unnecessary complication.

 

Store just the Celsius/Fahrenheit value and convert from and to the Fahrenheit/Celsius value when necessary.

Thanks for the input.

 

I now have this for set/get...

 

<?php

private $tempC = null;
private $tempF = null;

public function getTempC(){
	return $this->tempC;
}

public function setTempC($tempC){
	$this->tempC = $tempC;
}

public function getTempF(){
	return $this->tempF;
}

public function setTempF($tempF){
	$this->tempF = $tempF;
}

?>

 

And this for the class and the formulas...

 

<?php

      class Temp {

      public $tempC;
      public $tempF;
      
      function ConvertCF(){  
        $this->tempF = ($this->tempC / 5) * 9 + 32;
        return $this->tempF; 
      }

      function ConvertFC(){  
        $this->tempC = ($this->tempF - 32) / 9 * 5;
        return $this->tempC;  
      }
      
    } 
    
?>

 

But I still don't know how to bring these pieces together.

 

A big part of my problem is I don't really understand the get/set declaration.

 

Do I really need it?

 

Uh...both of your code snippets should be within the same class code, for starters (they may actually be, on your end, but the way you're writing your code is odd in this instance).

 

Second, like requinix said, there's no need to store different values for C, F, and k.  The temperature is the same, just represented differently.  Have the class do the heavy lifting.

 

class Temperature
{
   private $temperature;

   public function __construct($value, $units = "c")
   {
      $units = strtolower($units);

      if ($units !== "c")
      {
         $val = $this->convertTemp($val, $units);
      }

      $this->temperature = $val;
   }

   public function getTemp($units = "c")
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         return $this->temperature;
      }
      else
      {
         return $this->convertTemp($this->temperature, $units);
      }
   }

   public function setTemp($val, $units = "c")
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         $this->temperature = $val;
      }
      else
      {
         $this->temperature = $this->convertTemp($val, $units);
      }
   }

   private function convertTemp($val, $units = "c");
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         return $val;
      }
      else if ($units === "f")
      {
         return convertToFahrenheit($val);
      }
      else
      {
         return convertToKelvin($val);
      }
   }

   private function convertToFahrenheit($val)
   {
      // code to convert a temperature represented in Celsius to Fahrenheit
   }

   private function convertToKelvin($val)
   {
      // code to convert a temperature represented in Celsius to Kelvin
   }
}

 

This class stores its internal temperature as a measurement in Celsius, and does conversions to/from Fahrenheit and Kelvin respectively, when needed.  It's as simple to use as:

 

$temp = new Temperature(100); // boiling point of water in Celsius

echo "In Fahrenheit: " . $temp->getTemp("F");

 

It's certainly not a complete class (no error/exception handling), but it should give you some insight into how to approach the problem.

OK, I think I'm starting to understand at least some of this...

 

I don't need Kelvin, so I removed that.

 

I put the formulas in, but not totally sure I did that right.

 

Here's what I have at this point for the class:

 

	

<?php

class Temperature
{
   private $temperature;

   public function __construct($value, $units = "c")
   {
      $units = strtolower($units);

      if ($units !== "c")
      {
         $val = $this->convertTemp($val, $units);
      }

      $this->temperature = $val;
   }

   public function getTemp($units = "c")
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         return $this->temperature;
      }
      else
      {
         return $this->convertTemp($this->temperature, $units);
      }
   }

   public function setTemp($val, $units = "c")
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         $this->temperature = $val;
      }
      else
      {
         $this->temperature = $this->convertTemp($val, $units);
      }
   }

   private function convertTemp($val, $units = "c");
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         return $val;
      }
      else ($units === "f")
      {
         return convertToFahrenheit($val);
      }
   }

   private function convertToFahrenheit($val)
   {
      $this->val = ($this->val / 5) * 9 + 32;
   }
}

?>

 

Am I close?

This

      else ($units === "f")
      {
         return convertToFahrenheit($val);
      }

 

should be

      else ($units === "f")
      {
         return $this->convertToFahrenheit($val);
      }

 

plus you need to remove the semicolon after this function declaration

private function convertTemp($val, $units = "c"); // <--- there is extra semicolon that doesnt belong there

changed, but still nothing...

 

<?php

include ('tempconv.class.php');

$temp = new Temperature(100);

echo "In Fahrenheit: " . $temp->getTemp("f");

?>

 

<?php

class Temperature
{
   private $temperature;

   public function __construct($value, $units = "c")
   {
      $units = strtolower($units);

      if ($units !== "c")
      {
         $val = $this->convertTemp($val, $units);
      }

      $this->temperature = $val;
   }

   public function getTemp($units = "c")
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         return $this->temperature;
      }
      else
      {
         return $this->convertTemp($this->temperature, $units);
      }
   }

   public function setTemp($val, $units = "c")
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         $this->temperature = $val;
      }
      else
      {
         $this->temperature = $this->convertTemp($val, $units);
      }
   }

   private function convertTemp($val, $units = "c");
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         return $val;
      }
      else ($units === "f")
      {
         return $this->convertToFahrenheit($val);
      }
   }

   private function convertToFahrenheit($val)
   {
      $this->val = ($this->val / 5) * 9 + 32;
   }
}

?>

 

Appreciate you hanging in with me on this!

Try this in the beginning of your script

error_reporting(-1);
ini_set('display_errors', 1);

 

I also made a working example of your class (dosn't have too much validation for the input nor error checking to keep it simple)

class Temperature
{
private $temperature;

// Constructor
public function __construct($val)
{
	$this->temperature = $val;
}

// Function to set temp, $temperature inside class is always in celcius.
public function setTemp($val, $unit)
{
	$unit = strtolower($unit);
	if ($unit === 'c')
	{
		$this->temperature = $val;
	}
	else if ($unit === 'f')
	{
		// Convert fahrenheit to celsius. And assign it to class variable.
		$this->temperature = (($val - 32) / 1.;
	}
}

public function convertToFahrenheit()
{
	return ($this->temperature * 1. + 32;
}

public function getTemp($unit)
{
	$unit = strtolower($unit);
	if ($unit === 'c')
	{
		return $this->temperature;
	}
	else if ($unit === 'f')
	{
		return $this->convertToFahrenheit();
	}
	else
	{
		return 'Invalid unit type.';
	}
}
}



$temp = new Temperature(100);
echo "In Fahrenheit: " . $temp->getTemp('c') . '<br>';
echo "In Fahrenheit: " . $temp->getTemp('f') . '<br>';
echo "In Fahrenheit: " . $temp->getTemp('X') . '<br>';

Aside from a couple of typos (an extraneous semicolon at the end of convertTemp's signature, and forgetting to put $this->convertToFahrenheit in the same method), it works fine for me.

 

EDIT: My code with the kelvin conversion removed -

 

class Temperature
{
   private $temperature;

   public function __construct($value, $units = "c")
   {
      $units = strtolower($units);

      if ($units !== "c")
      {
         $val = $this->convertTemp($val, $units);
      }

      $this->temperature = $val;
   }

   public function getTemp($units = "c")
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         return $this->temperature;
      }
      else
      {
         return $this->convertTemp($this->temperature, $units);
      }
   }

   public function setTemp($val, $units = "c")
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         $this->temperature = $val;
      }
      else
      {
         $this->temperature = $this->convertTemp($val, $units);
      }
   }

   private function convertTemp($val, $units = "c")
   {
      $units = strtolower($units);

      if ($units === "c")
      {
         return $val;
      }
      else if ($units === "f")
      {
         return $this->convertToFahrenheit($val);
      }
   }

   private function convertToFahrenheit($val)
   {
     return ($val / 5) * 9 + 32;
   }
}

So far, it's looking good to me.  I'd add the units as class constants, though, to prevent typos:

 

class Temperature
{
   const FAHRENHEIT = 'f';
   const CELSIUS = 'c';

   private $temperature;

   public function __construct($value, $units = self::CELSIUS)
   {
      if ($units !== self::CELSIUS)
      {
         $val = $this->convertTemp($val, $units);
      }

      $this->temperature = $val;
   }

   public function getTemp($units = self::CELSIUS)
   {
      if ($units === self::CELSIUS)
      {
         return $this->temperature;
      }
      else
      {
         return $this->convertTemp($this->temperature, $units);
      }
   }

   public function setTemp($val, $units = self::CELSIUS)
   {
      if ($units === self::CELSIUS)
      {
         $this->temperature = $val;
      }
      else
      {
         $this->temperature = $this->convertTemp($val, $units);
      }
   }

   private function convertTemp($val, $units = self::CELSIUS)
   {
      if ($units === self::CELSIUS)
      {
         return $val;
      }
      else if ($units === self::FAHRENHEIT)
      {
         return $this->convertToFahrenheit($val);
      }
   }

   private function convertToFahrenheit($val)
   {
     return ($val / 5) * 9 + 32;
   }
}

 

Then you can call it like:

$temperature = new Temperature(85, Temperature::FAHRENHEIT);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.