Jump to content

Simple Calss in PHP HELPPP..!


kathir

Recommended Posts

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.:(

Link to comment
https://forums.phpfreaks.com/topic/287419-simple-calss-in-php-helppp/
Share on other sites

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.

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;
}

 

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... :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.