Understanding public and private properties and methods in OOP PHP

Wahyu Raja Butar-Butar Reply 09:51
Properties and methods can be public or private. Public means that methods or properties inside the class can be accessed by the script that is using the class or from another class. For example, the following class has a public property and a public method:
class Car
{
 public $gas = 0;
 function addGas($amount)
 {
  $this->gas = $this->gas + $amount;
  echo "$amount gallons added to gas tank";
 }
}
The public property in this class can be accessed by a statement in the script outside the class, as follows:
$mycar = new Car;
$gas_amount = $mycar->gas;
After these statements are run, $gas_amount contains the value stored in $car inside the object. The property can also be modified from outside the class, as follows:
$mycar->gas = 20;
Allowing script statements outside the class to directly access the properties of an object is poor programming practice. All interaction between the object and the script or other classes should take place using methods. The example class has a method to add gas to the car. All gas should be added to the car by using the addGas method, which is also public, using statements similar to the following:
$new_car = new Car;
$new_car->addGas(5);
You can prevent access to properties by making them private, as follows:
private $gas = 0;

With the property specified as private, a statement in the script that attempts to access the property directly, as follows:

$myCar->gas = 20;
gets the following error message: Fatal error: Cannot access private property car::$gas in c:\ testclass.php on line 17
Now, the only way gas can be added to the car is by using the addGas method. Because the addGas method is part of the class statement, it can access the private property. In the same way, you can make methods private or protected. In this case, you want the outside world to use the addGas method. However, you might want to be sure that people buy the gas that is added. You don’t want any stolen gas in the car. You can write the following class:
class Car
{
 private $gas = 0;
 private function addGas($amount)
 {
  $this->gas = $this->gas + $amount;
  echo "$amount gallons added to gas tank";
 }
 function buyGas($amount)
 {
  $this->addGas($amount);
 }
}
With this class, the only way gas can be added to the car from the outside is with the buyGas method. The buyGas method uses the addGas method to add gas to the car, but the addGas method can’t be used outside the class because it’s private. If a statement outside the class attempts to use addGas, as follows, a fatal error is displayed, as it was for the private property:
$new_car = new Car;
$new_car->addGas(5);
However, a statement outside the class can now add gas to the car by using the buyGas method, as follows:
$new_car = new Car;
$new_car->buyGas(5);
You see the following output: 5 gallons added to gas tank It’s good programming practice to hide as much of your class as possible. Make all properties private. You should make methods public only if they absolutely need to be public.

Related Posts

PHP 5121803647785991335
Comments
0 Comments
Facebook Comments by Media Blogger

Post a Comment

Search

Google+ Followers

Popular Posts

Translate