|
20-maruza: phpda obyektga yonaltirilgan dasturlash. Mvc texnologiyasi
|
bet | 4/6 | Sana | 30.01.2024 | Hajmi | 20,38 Kb. | | #148601 |
Bog'liq 20-maruza phpda obyektga yo’naltirilgan dasturlash. Mvc texnolo-www.kompy.infoSinf metodlarini o'rnatish
class User {
private $name;
private $gender;
public $age;
private $department = 'Mechanical';
/* set value to 'private $name' property */
public function setName($name){
$this->name = $name;
}
/* get value from 'private $name' property */
public function getName(){
return $this->name;
}
}
To'liq sinf
class User {
private $name;
private $gender;
public $age;
private $department = 'Mechanical';
public function setName($name){
$this->name = $name;
}
public function getName(){
return $this->name;
}
public function setGender($gender){
$this->gender = $gender;
}
public function getGender(){
return $this->gender;
}
public function setDepartment($dep){
$this->department = $dep;
}
public function getDepartment(){
return $this->department;
}
}
$user = new User();
Obyekt o'zgaruvchilariga qiymat berish
$user->setName('Sheri');
$user->setGender('F');
//public property : no error, accessible from anywhere
$user->age = 30;
//Fatal error: Cannot access private property
//Private and protected methods can not be access directly
$user->department = 'Computer';
//The right way to assign value for protected and private methods
$user->setDepartment('Computer');
Obyekt o'zgaruvchilarining qiymati o'qish va chop qilish
echo $user->getName();
echo $user->getDepartment();
echo $user->age;
//Fatal error: Cannot access private property User::$department
echo $user->department;
/* You can make as many instance as you want */
$user2 = new User();
//prints Mechanical (default value for all instances)
echo $user2->getDepartment();
//changed the default department
$user2->setDepartment('Computer');
|
| |