Facebook

Object Oriented Calculator in Php

Object Oriented Calculator in Php

in this article we will learn how to make calculator in object oriented paradigm we can make calculator in procedural concept its super easy but now aday every one work in object oriented paradigm this calculator super easy for you because this article only for begginer level student who understand a little bit object oriented paradigm so lets go to start 

one=$one;
      $this->two=$two;
  return $this->one + $this->two;
 }
 }
 public function sub($one,$two)
 {
  if(empty($one) && empty($two))
  {
   echo 'please supply two numbers';
  }
  else
  {
  $this->one=$one;
  $this->two=$two;
  return $this->one - $this->two;
 }
 }
 public function mul($one,$two)
 {
  if(empty($one) && empty($two))
  {
   echo 'please supply two numbers';
  }
  else
  {
  $this->one=$one;
  $this->two=$two;
  return $this->one * $this->two;
 }}
 public function divide($one,$two)
 {
  if(empty($one) && empty($two))
  {
   echo 'please supply two numbers';
  }
  else
  {
  $this->one=$one;
  $this->two=$two;
  return $this->one / $this->two;
 }}
}
$obj=new calculator();

echo $obj->add(4,5);
echo '
'; echo $obj->sub(56,4); echo '
'; echo $obj->mul(45,6); echo '
'; echo $obj->divide(45,5); ?>

Explanation : 

the class name is calculator and we well access all methods using class object so the name of first method is add and we will also pass values from call method after calss object is created the first method will be call and 4 and 5 values pass from method thats values will goes to add method and here simple add with each other and and similarly subtrictor method i mean sub method wil call we pass two values 56 and 4 from sub method and similarly we just subtract 56 from 4 and then we call multiplication method means mul method and pass two values 45 and 6 similary multiply with each other and we have also validate every function if user pass no values then we disply message to user please supply two values so this is very basic object oriented calculator in php this is spacially for begginer level object oriented paradigm student who are begginer in object oriented paradigm cocept
Object Oriented Calculator in Php Object Oriented Calculator in Php Reviewed by Shakil Khan on 06:30 Rating: 5

4 comments:

  1. nice.. I like it.. it's kinda an interesting way of knowing OBJECTS in php. :)

    Thanks

    ReplyDelete
  2. This ins't Calculator in oriented object, is only a class with 4 method for calculate. Look for Interpreter pattern for calculate truly.

    ReplyDelete
    Replies
    1. MASTERS: this article only for begginer level student i know its not a good approach its not for intermediate students

      Delete

Thanks for your feedback...

Useful Information

Powered by Blogger.