Tuesday, March 30, 2010

Advanced PHP - part 2

We will learn about the access modifiers available to PHP classes. Like any other programming languge , we have public ( acessible to all ) , private ( acessible to methods inside the class ) and protected ( classes derived from it can access ..besides methods inside ).



<?php
class Test {
////////////////////////////////////////
// Private variables are only accessible to
// methods inside the class
private $d1;
////////////////////////////////////////
// Public variables are available for anyone
//
public $d2;
////////////////////////////////////////
// Protected variables are available to
// all the methods inside the class and
// and classes inherited from it..
protected $d3;

public function Dump () {
echo $this->d3;
echo "\n";
$this->d2=400;
echo $this->d2;
echo "\n";
$this->d1=200;
echo $this->d1;
}

};

////////////////////////////////////
//
// Test2 inherits from Test..it can access
// protected members of the base...
class Test2 extends Test
{
public function Set( $val ) {
$this->d3 = $val;
}
};


$test_inst = new Test2();
$test_inst->Set(10);
$test_inst->Dump();


?>


[sandhya@localhost newoop]$ php -f second2.cpp
10
400
200[sandhya@localhost newoop]$

1 comments:

Anonymous said...

The Christmas travel plans of thousands of are in disarray after snow left Heathrow Airport all but shut. Thousands have been forced to sleep overnight in airports and there is disruption to road and rail travel with the Met Office warning of more snow ...