calculator class and to learn about Scopes.
so first let's make a new class and call it Calc.
<?php class Calc { public $num; public $num2; public $output; function setNum($input) { $this->num = (int) $input; } function setNum2($input) { $this->num2 = (int) $input; } } ?>
In our class I've added 3 public variables :
public $num; public $num2; public $output;
this public variables or global variables or whatever you want to call them.. will be used to store the values of the numbers that we are going to calculate and the answer.
After the variables I've created 2 functions :
function setNum($input) { $this->num = (int) $input; } function setNum2($input) { $this->num2 = (int) $input; }
setNum($input) - we are going to use this function to set the value of the public $num variable. $input is a parameter. Why did I put (int) before the $input? It's called Typecasting


let's add some basic math operations functions into our class Calc now..
<?php class Calc { public $num; public $num2; public $output; function setNum($input) { $this->num = (int) $input; } function setNum2($input) { $this->num2 = (int) $input; } function addNum() { $this->output = $this->num + $this->num2; } function subtractNum() { $this->output = $this->num - $this->num2; } function multiplyNum() { $this->output = $this->num * $this->num2; } function divideNum() { $this->output = $this->num / $this->num2; } } ?>
we now have a Add, Subtract, Multiply and Divide functions that we can use to calculate our 2 numbers

function addNum() { $this->output = $this->num + $this->num2; } function subtractNum() { $this->output = $this->num - $this->num2; } function multiplyNum() { $this->output = $this->num * $this->num2; } function divideNum() { $this->output = $this->num / $this->num2; }
Now let's create a function that will echo out our $output

<?php class Calc { public $num; public $num2; public $output; function setNum($input) { $this->num = (int) $input; } function setNum2($input) { $this->num2 = (int) $input; } function addNum() { $this->output = $this->num + $this->num2; } function subtractNum() { $this->output = $this->num - $this->num2; } function multiplyNum() { $this->output = $this->num * $this->num2; } function divideNum() { $this->output = $this->num / $this->num2; } function getResult() { echo $this->output; } } ?>
here's the function that I've just added
function getResult() { echo $this->output; }
so everything is ok now.. let's now use our class

<?php class Calc { public $num; public $num2; public $output; function setNum($input) { $this->num = (int) $input; } function setNum2($input) { $this->num2 = (int) $input; } function addNum() { $this->output = $this->num + $this->num2; } function subtractNum() { $this->output = $this->num - $this->num2; } function multiplyNum() { $this->output = $this->num * $this->num2; } function divideNum() { $this->output = $this->num / $this->num2; } function getResult() { echo $this->output; } } $calc = new Calc(); $calc->setNum(1); $calc->setNum2(2); $calc->addNum(); $calc->getResult(); ?>
output :
3
Some explanation on what happened..
First we've instantiate our calc
$calc = new Calc();
Then we set the value for the $num and $num2
$calc->setNum(1); ]$calc->setNum2(2);
Then we've called the addNum function to add the $num and $num2 and store the result into the variable called $output
$calc->addNum();
Then we want to know the output that's why we called the function :
$calc->getResult();
Let's go now into the SCOPE thing.. everything in our class is now public so that means everything can be changed and access.. for example I can assign a value into the $output variable
$calc->output = 5;
and we don't want the user to assign a value into the out and to the $num and $num2 what are we going to do? That's what SCOPE is.. SCOPE is where you limit the members of a class like functions or variables.. if you want them to be access outside the class or in other class, you declare them as public... if you don't want you make them private or protected.
What is the difference of private and protected? Private Variables of functions can only be accessed inside the class.. while Protected variables and functions can be accessed inside the class and inside the child classes

private $num; private $num2; private $output;
We are now done

We have a very very simple calculator now...
More Powers
Papabear
Previous Tutorial :
Properties and $this - Part 2
Next Tutorial :
Creating a File Logger - Part4