access modifiers
Let's test the access modifiers we learned earlier.
protected test
protected can also be used in declared classes, and can also be used in inherited child classes.
<?php class parents { protected $parents = "Parental car"; } class child extends parents { public function rentCar() { return "Borrow a {$this->parents}"; } } $test = new child; echo $test->rentCar(); ?>
Result of the code above
The above code declared a property as protected in the parent class. So you could use that property in a child class.
This time, I'll change protected to private in the same code.
Then, of course, it is a property that can only be used in a parent class, so it cannot be used in a child class.
<?php class parents { private $parents = "Parental car"; } class child extends parents { public function rentCar() { if(isset($this->parents)){ return "Borrow a {$this->parents}"; } else { return "I can't rent a car."; } } } $test = new child; echo $test->rentCar(); ?>
Result of the code above
The isset function determines whether the variable is present and returns true if it exists and false if it does not exist.