kathir Posted March 31, 2014 Share Posted March 31, 2014 Hello Frieds, I have problem with simple class in php. Please Help. This is my form code. <form name="form1" action="ex1_exec.php" method="POST" /> <input type="text" name="val1" /> <input type="submit" name="submit" value="Submit" /> </form> and, the ex1_exec.php file code is, <?php public $val1=$_POST['val1']; class tempclass { return $this->val1+5; } $obj=new tempclass($val1); echo $obj->val1; ?> When try to run those codes, i get only empty page. Please Help for this problem. Quote Link to comment https://forums.phpfreaks.com/topic/287419-simple-calss-in-php-helppp/ Share on other sites More sharing options...
ginerjm Posted March 31, 2014 Share Posted March 31, 2014 Turn on error checking. Quote Link to comment https://forums.phpfreaks.com/topic/287419-simple-calss-in-php-helppp/#findComment-1474527 Share on other sites More sharing options...
kathir Posted March 31, 2014 Author Share Posted March 31, 2014 Turn on error checking. i did it..but no change.. Quote Link to comment https://forums.phpfreaks.com/topic/287419-simple-calss-in-php-helppp/#findComment-1474559 Share on other sites More sharing options...
kathir Posted March 31, 2014 Author Share Posted March 31, 2014 no one solved it for me.. Quote Link to comment https://forums.phpfreaks.com/topic/287419-simple-calss-in-php-helppp/#findComment-1474560 Share on other sites More sharing options...
ginerjm Posted March 31, 2014 Share Posted March 31, 2014 Can we see it where you added the error checking? Quote Link to comment https://forums.phpfreaks.com/topic/287419-simple-calss-in-php-helppp/#findComment-1474562 Share on other sites More sharing options...
ginerjm Posted March 31, 2014 Share Posted March 31, 2014 Notes: 1 - you defined a public var called val1 but it is not part of your class. Correct? 2 - you defined the class and included an executable line. I do not know how that fits since it is not part of a constructor. Of course, I'm not a big OO guy. Also - you have no methods for this class. 3 - you attempt to display a property of the class that is not defined for the class. Got to be an error. Quote Link to comment https://forums.phpfreaks.com/topic/287419-simple-calss-in-php-helppp/#findComment-1474564 Share on other sites More sharing options...
iarp Posted April 1, 2014 Share Posted April 1, 2014 Classes don't really work that way in terms of the direct return statement. Your class should look more like this: class tempclass { public $val1; function __construct($my_var) { $this->val1 = $my_var+5; } } but really it looks like you want a function more than a class function my_func($my_var) { return $my_var+5; } Quote Link to comment https://forums.phpfreaks.com/topic/287419-simple-calss-in-php-helppp/#findComment-1474600 Share on other sites More sharing options...
Solution kathir Posted April 1, 2014 Author Solution Share Posted April 1, 2014 Classes don't really work that way in terms of the direct return statement. Your class should look more like this: class tempclass { public $val1; function __construct($my_var) { $this->val1 = $my_var+5; } } but really it looks like you want a function more than a class function my_func($my_var) { return $my_var+5; } wow..Thanks a lot.. am a new bee and it helps me a lot... Quote Link to comment https://forums.phpfreaks.com/topic/287419-simple-calss-in-php-helppp/#findComment-1474613 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.