Visibility and Constructors
Private Property
A private property can only be changed from inside the class.
Private Property
private_property.php
<?php
class ScoreBox
{
private int $score = 0;
public function add(int $points): int
{
$this->score = $this->score + $points;
return $this->score;
}
}
$points = ;
$box = new ScoreBox();
$total = $box->add($points);
echo "points=" . $points . "\n";
echo "total=" . $total . "\n";
<?php
class ScoreBox
{
private int $score = 0;
public function add(int $points): int
{
$this->score = $this->score + $points;
return $this->score;
}
}
$points = ;
$box = new ScoreBox();
$total = $box->add($points);
echo "points=" . $points . "\n";
echo "total=" . $total . "\n";
<?php
class ScoreBox
{
private int $score = 0;
public function add(int $points): int
{
$this->score = $this->score + $points;
return $this->score;
}
}
$points = ;
$box = new ScoreBox();
$total = $box->add($points);
echo "points=" . $points . "\n";
echo "total=" . $total . "\n";
hidden state
Private state lets the class control how a value changes.