• Toliq sinf
  • Sinf misollar
  • Obyekt ozgaruvchilarining qiymati oqish va chop qilish
  • 20-maruza: phpda obyektga yonaltirilgan dasturlash. Mvc texnologiyasi




    Download 20,38 Kb.
    bet4/6
    Sana30.01.2024
    Hajmi20,38 Kb.
    #148601
    1   2   3   4   5   6
    Bog'liq
    20-maruza phpda obyektga yo’naltirilgan dasturlash. Mvc texnolo-www.kompy.info

    Sinf 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;
    }
    }

    Sinf misollar


    $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');

    Download 20,38 Kb.
    1   2   3   4   5   6




    Download 20,38 Kb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    20-maruza: phpda obyektga yonaltirilgan dasturlash. Mvc texnologiyasi

    Download 20,38 Kb.