Jump to content

add two nos using constructors


srinivasa

Recommended Posts

well you need to check whether the numbers were sent using POST or not, in the case where nothing was sent you will get errors as the POST variables are filled with nothing.

 

one way would be checking whether anything was sent:

 


{
// Inside your class

private $no1 = 0;
private $no2 = 0;

public function __construct(){

if( isset($_POST['no1'] ) && isset( $_POST['no2'] ) ) {

$this->no1 = $_POST['no1'];
$this->no2 = $_POST['no2'];

} 

}

public function add() {
return $this->no1 + $this->no2;
}

public function subtract(){
return $this->no1 - $this->no2;
}

// your class ends
}

$test = new nameofclass();

echo $test->add();
echo $test->subtract();


 

of course you could add more stuff like sending them back to the form page if they didn't send anything and so on.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...