Pain Posted October 8, 2012 Share Posted October 8, 2012 Hello. I am fairly new to the object orientated php. Just wondering if I could get any help with this one. With this code I am attempting to convert a string to upper case. <?php class Product{ public $type = "Milk"; public function convertToUpperCase(){ strtoupper($type); } } $product = new Product; echo $product->type; echo $product->convertToUpperCase($type); ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/ Share on other sites More sharing options...
Pikachu2000 Posted October 8, 2012 Share Posted October 8, 2012 Fantastic. Do you have a question? Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383803 Share on other sites More sharing options...
Pain Posted October 8, 2012 Author Share Posted October 8, 2012 It does not echo out the string in upper case. Where did I go wrong? Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383804 Share on other sites More sharing options...
Pikachu2000 Posted October 8, 2012 Share Posted October 8, 2012 Turn on error reporting, and post any errors you get. Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383807 Share on other sites More sharing options...
Pain Posted October 8, 2012 Author Share Posted October 8, 2012 Notice: Undefined variable: type in /home/searchqu/public_html/ww3/main.php on line 14 Notice: Undefined variable: type in /home/searchqu/public_html/ww3/main.php on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383810 Share on other sites More sharing options...
ManiacDan Posted October 8, 2012 Share Posted October 8, 2012 Class variables are accessed with $this->type; Not just $type; Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383812 Share on other sites More sharing options...
Barand Posted October 8, 2012 Share Posted October 8, 2012 try public function convertToUpperCase(){ $this->type = strtoupper($this->type); } Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383813 Share on other sites More sharing options...
TOA Posted October 8, 2012 Share Posted October 8, 2012 (edited) *Edit* Beat me to it. Also, you'll need to return from convertToUppercase or alternatively echo type again after converting. Edited October 8, 2012 by TOA Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383814 Share on other sites More sharing options...
Pain Posted October 8, 2012 Author Share Posted October 8, 2012 Ok im starting to pick things up. I've adjusted the code to <?php error_reporting(-1); class Product{ public $type = "Milk"; public function convertToUpperCase(){ $this->type = strtoupper($this->type); return $this->type; } } $product = new Product; echo $product->type; echo $product->convertToUpperCase($type); ?> It now does work, but still produces an error: Notice: Undefined variable: type in /home/searchqu/public_html/ww3/main.php on line 15 So there is still something not quite right with this line. echo $product->convertToUpperCase($type); Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383820 Share on other sites More sharing options...
Jessica Posted October 8, 2012 Share Posted October 8, 2012 1. Your method doesn't accept any arguments, so it's useless to pass it one. 2. At that point in the code, $type is undefined. Yes. Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383821 Share on other sites More sharing options...
TOA Posted October 8, 2012 Share Posted October 8, 2012 (edited) *Edit* Beat again! I really miss the alert that used to come up (maybe I'm just not seeing it ! Edited October 8, 2012 by TOA Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383822 Share on other sites More sharing options...
Pain Posted October 8, 2012 Author Share Posted October 8, 2012 Thanks, that works just fine. Still got one q though. If i were to use a POST variable, how should it look like? Should i assign POST to variables INSIDE the class? Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383825 Share on other sites More sharing options...
Barand Posted October 8, 2012 Share Posted October 8, 2012 How would you like it to look - a POST variable for what? No, not inside the class. Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383830 Share on other sites More sharing options...
Pain Posted October 8, 2012 Author Share Posted October 8, 2012 (edited) I want to input some text into a textbox and then use that input in the class. Not sure how to even start, couldn't find any decent tuts on that. Edited October 8, 2012 by Pain Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383840 Share on other sites More sharing options...
TOA Posted October 9, 2012 Share Posted October 9, 2012 Then you need to pass it into the method. Something like this class SomeClass { public function useVar($var) { //do something with $var } } $someval = $_POST['someval']; $obj = new SomeClass; $obj->useVar($somevar); Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383852 Share on other sites More sharing options...
Pain Posted October 9, 2012 Author Share Posted October 9, 2012 A bit confused... <form method="POST"> <input type="text" name="someval" /> <input type="submit" name="submit" /> </form> <?php error_reporting(-1); class Product{ public function convertToUpperCase($var){ if(isset($submit)) { $this->var = strtoupper($this->var); return $this->var; } } } $someval = $_POST['someval']; $submit = $_POST['submit']; $product = new Product; echo $product->convertToUpperCase($someval); ?> Once again i get the error of not defining variables: Notice: Undefined index: someval in /home/searchqu/public_html/ww3/main.php on line 22 Notice: Undefined index: submit in /home/searchqu/public_html/ww3/main.php on line 23 And when i input some text nothing happens.:/ Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383913 Share on other sites More sharing options...
TOA Posted October 9, 2012 Share Posted October 9, 2012 (edited) 1. You need a class varaible to hold $this->var. Add public $var; as the first thing in your class definition. 2. You can't use $submit in the class without passing it in. You could potentially check $_POST there since it's a global though. 3. Your script structure is all f'd up. But one thing at a time. Edited October 9, 2012 by TOA Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383916 Share on other sites More sharing options...
TOA Posted October 9, 2012 Share Posted October 9, 2012 (edited) I didn't test this, but try this <?php // handle processing and declarations first so you don't get header errors error_reporting(-1); class Product{ public $var; public function convertToUpperCase($var){ if(isset($_POST['submit'])){ $this->var = strtoupper($var); return $this->var; } } } if ($_SERVER['REQUEST_METHOD'] == "POST") { // process the form $someval = $_POST['someval']; $product = new Product; echo $product->convertToUpperCase($someval); } else { //display the form <form method="POST"> <input type="text" name="someval" /> <input type="submit" name="submit" /> </form> } ?> Edited October 9, 2012 by TOA Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383920 Share on other sites More sharing options...
Pain Posted October 9, 2012 Author Share Posted October 9, 2012 Still gives me an error - Notice: Undefined index: someval in /home/searchqu/public_html/ww3/main.php on line 17 Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383923 Share on other sites More sharing options...
ManiacDan Posted October 9, 2012 Share Posted October 9, 2012 The code you're given on a forum may just be an example, and not designed to be blindly copied and pasted into your application without any problems. If you'd like to blindly copy and paste without reading it and learning how it works, we have a freelancer forum where you can hire employees. 'someval' was used in that code to indicate some value, a value you need to provide. Quote Link to comment https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383926 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.