On Fri, Oct 8, 2010 at 1:24 PM, sri vats <[email protected]> wrote:
> In PHP i have created a global variable.
>
> global $x
>
> Instead defining this global variable in each method of a class,I like to
> define the variable in class and make it available for all the methods
> inside the same class.Your suggestions are welcome.
>
Instead of using a variable as global, declare a variable in a class and use
it in all the methods (functions) within the class as given below. create an
object and then call the methods with the required parameters using the
object.
<?php
Class var1 // declaring a class
{
var $a; //declaring a variable in a class
function incr($par) //method1 using the class variable "a"
{
$this->a = $par; // assigning the value of the parameter to the
class variable "a"
echo $this->a; // output the class variable "a" using "this"
operator
}
function incr1() //method2 using the class variable "a"
{
echo $this->a; //here "a" is globally accessed
}
}
$ob1 = new var1; //creating an object
$ob1->incr(100); // calling method1 using the object "ob1"
with one parameter "100"
$ob1->incr1(); // calling method2 using the same object
"ob1"
?>
Hope this helps answering your query.
Thanks,
_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc