Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * - 1 votes

PHP OOP Tutorial from beginner to advance - (Scope and Calculator - Part 3)

creating a calculator variable scope function scope php oop object oriented php class variable type calculator

  • Please log in to reply
3 replies to this topic

#1 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 28 September 2012 - 07:13 AM

Hello everyone and welcome into my PHP OOP Tutorial part 3 and today we are going to make a very very simple
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 :) Why? Because we all know that PHP is a loosely typed language and assigns types to variable depending what is assigned to it. What is Typecasting by the way? It is a way of casting the variable type like integer, string, double etc... we are working on whole numbers so we have to make sure that $input is a string so we typecast it :)

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 :D



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 :) were going to tackle about child classes soon..



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
  • 1
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 28 September 2012 - 08:13 AM

Usually, based on the OOP way of enclosement and abstraction, you don't do any members (variables) public unless you really have to. Protected is fine too, if you build a parent etc, but public is to be avoided. The members are accessed by setters and getters, methods which returns or sets the value of a member, often with some security so the member have the right values and stuff.
  • 1

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 indinity

indinity

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Location:bangladesh
  • Programming Language:C, C++
  • Learning:PHP, JavaScript

Posted 28 April 2014 - 12:55 PM

fantastic.


  • 0

#4 arora

arora

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Location:lucknow
  • Programming Language:PHP
  • Learning:PHP

Posted 19 June 2014 - 03:42 AM

thank svery  much  :thumbup:


  • 0





Also tagged with one or more of these keywords: creating a calculator, variable scope, function scope, php oop, object oriented, php class, variable type, calculator