webguy262 Posted August 10, 2011 Share Posted August 10, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/ Share on other sites More sharing options...
AbraCadaver Posted August 10, 2011 Share Posted August 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255565 Share on other sites More sharing options...
requinix Posted August 10, 2011 Share Posted August 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255570 Share on other sites More sharing options...
webguy262 Posted August 10, 2011 Author Share Posted August 10, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255585 Share on other sites More sharing options...
KevinM1 Posted August 10, 2011 Share Posted August 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255618 Share on other sites More sharing options...
webguy262 Posted August 11, 2011 Author Share Posted August 11, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255849 Share on other sites More sharing options...
KevinM1 Posted August 11, 2011 Share Posted August 11, 2011 It looks like it to me. Have you tried using it? Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255900 Share on other sites More sharing options...
webguy262 Posted August 11, 2011 Author Share Posted August 11, 2011 I am trying with this... <?php include ('tempconv.class.php'); $temp = new Temperature(100); echo "In Fahrenheit: " . $temp->getTemp("f"); ?> ...but I just get a blank page. What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255940 Share on other sites More sharing options...
TeNDoLLA Posted August 11, 2011 Share Posted August 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255953 Share on other sites More sharing options...
webguy262 Posted August 11, 2011 Author Share Posted August 11, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255963 Share on other sites More sharing options...
TeNDoLLA Posted August 11, 2011 Share Posted August 11, 2011 Do you have error reporting turned on ? Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255965 Share on other sites More sharing options...
webguy262 Posted August 11, 2011 Author Share Posted August 11, 2011 tried this... <?php error_reporting(E_ALL); include ('tempconv.class.php'); $temp = new Temperature(100); echo "In Fahrenheit: " . $temp->getTemp("f"); ?> ...but i don't get anything Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255970 Share on other sites More sharing options...
TeNDoLLA Posted August 11, 2011 Share Posted August 11, 2011 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255974 Share on other sites More sharing options...
KevinM1 Posted August 11, 2011 Share Posted August 11, 2011 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1255986 Share on other sites More sharing options...
shlumph Posted August 11, 2011 Share Posted August 11, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1256008 Share on other sites More sharing options...
webguy262 Posted August 11, 2011 Author Share Posted August 11, 2011 Thanks to all. Now I just need to pore over all of it and try to learn what I don't understand! Quote Link to comment https://forums.phpfreaks.com/topic/244448-trying-to-write-my-first-class/#findComment-1256015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.