View Single Post
  #32 (permalink)  
Old 04-28-2008, 07:03 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Lightbulb Re: PHP Classes For Beginners

Public variables and functions are accessible from anywhere in your script.

We can see in the below example that the public variable $num is set from user space and we call a public method that adds two the number and returns the value of $num+2. Having our properties (variables) visible or accessible from any part of our script works in our favors here, but it can also work against us. A could arise if we lost track of our values and changed the value of $num. To counter this problem we can create a method to set the value for us. Even with this in place it is still possible for somebody to simply access the $num variable. So we make the variable private. This ensures us that the property is only available within the class itself. It is private to the calling class.
PHP Code:

<?php

class mathematics
{
    
/*** a number ***/
    
public $num;
    public function 
addTwo()
       {
         Return 
$this->num+2;
    }
}
$math = new mathematics;

$math->num 2;

echo 
$math->addTwo();

?>
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Reply With Quote