Object Oriented Calculator in Php
Object Oriented Calculator in Php
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
Reviewed by Shakil Khan
on
06:30
Rating:
nice.. I like it.. it's kinda an interesting way of knowing OBJECTS in php. :)
ReplyDeleteThanks
thank you muhammad faroozi................
DeleteThis ins't Calculator in oriented object, is only a class with 4 method for calculate. Look for Interpreter pattern for calculate truly.
ReplyDeleteMASTERS: this article only for begginer level student i know its not a good approach its not for intermediate students
Delete