Jump to content

Simple Calss in PHP HELPPP..!


Go to solution Solved by 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;
}
  • Solution

 

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

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.