با سلام و احترام
چند مثال قرار دادم برای درک بیشتر، هرچند که این بخش، بخش پیچیدهای نیست ولی خب میتونید با خوندن کدهای متفاوت، درک بهتری پیدا کنید.
کدهارو به دو روش قرار میدم؛ اگر مایل به تست بودید، سعی کنید فایل رو دانلود کنید. درصورت کپی کردن، گاهی باگ ایجاد میشه.
موفق باشید.
/*
* Parrent Class
*/
namespace App;
abstract class Client
{
# Peroperties:
protected string $name;
protected string $operatingSystem;
protected string $location;
# Methods :
# Setters:
public function setName($name)
{
$this->name = $name;
}
public function setOperatingSystem($operatingSystem)
{
if (strlen($operatingSystem) >= 3)
$this->operatingSystem = $operatingSystem;
}
public function setLocation($location)
{
if (strlen($location) >= 3)
$this->location = $location;
}
# Getters:
public function getName()
{
return $this->name . PHP_EOL;
}
public function getOperatingSystem()
{
return $this->operatingSystem . PHP_EOL;
}
public function getLocation()
{
return $this->location . PHP_EOL;
}
}
/*
* Child Class
*/
class Mobile extends Client
{
# Inheritance
}
$mobile = new Mobile();
$mobile->setName('Mobile');
$mobile->setOperatingSystem('IOS');
$mobile->setLocation('In USA');
print_r("{$mobile->getName()} {$mobile->getOperatingSystem()} {$mobile->getLocation()}");
/*
* Child Class
*/
class Laptop extends Client
{
# Inheritance
}
$laptop = new Laptop();
$laptop->setName('LapTop');
print_r($laptop->getName()) ;