mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-08-05 06:17:37 +02:00
Working on inheritance page
This commit is contained in:
@@ -33,11 +33,28 @@ class Phone2
|
||||
}
|
||||
}
|
||||
|
||||
// The "protected" and "private" keywords work a little differently, but we
|
||||
// will learn more about "protected" when we discuss inheritance.
|
||||
// However, they both prevent functions and properties from being accessed outside an object.
|
||||
// The "protected" and "private" keywords work a little differently.
|
||||
// They both prevent functions and properties from being accessed outside an object.
|
||||
// However, a method or property marked "protected" can still be accessed by a child object.
|
||||
class Phone3
|
||||
{
|
||||
private $number;
|
||||
|
||||
protected $caller;
|
||||
|
||||
public function setNumber($number)
|
||||
{
|
||||
$this->number = $number;
|
||||
}
|
||||
}
|
||||
|
||||
// In class "Smartphone", the "caller" property is accessible because the parent class
|
||||
// has it marked as "protected". However, "Smartphone" cannot access the the "number" property
|
||||
// because it is still listed as private.
|
||||
class Smartphone extends Phone3
|
||||
{
|
||||
public function setCaller($caller)
|
||||
{
|
||||
$this->caller = $caller;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user