PHP Object-oriented encapsulation

Source: Internet
Author: User

<body><?PHP//concept of class//object concept//Definition class//class ren//{//Member variable//member method//}//object//$r = new Ren ();//The member of the calling object $r->//three main features: encapsulation, inheritance, polymorphic//1. Package// Purpose: To make the class more secure. Practice: Do not allow direct access to the members of the class//specific practices://1. The member becomes private: the access modifier (Public private protected)//2. Causes the member method to manipulate the variable//3. Use the __get () and __set () methods provided in the classclassren{Private $age;//Private Members    Private $name; Private $sex; //constructor: Initializes the member variable when the object is created//1. Execution time is special: The//2 is executed automatically when the object is created. Special: __construct        function__construct ($s)    {        $this->sex =$s; }        //to assign values to variable $age    /*Public function Setage ($a) {if ($a >18 and $a <50) {$this->age = $a;    }}//Take value function Getage () {return $this->age; }*/                //the method of assigning a value to a variable    function__set ($n,$v)    {        if($n= = "Age")        {            if($v>18 and$v<50)            {                $this-$n=$v; }        }        Else        {            $this-$n=$v; }    }    //method of taking value    function__get ($n)    {        return $this-$n; }        }$r=NewRen ("Male");$r->name = "Zhang San";Var_dump($r);//design a class: Contains $ A, $b, sum method, the method of product, you can initialize the variable, $a, $b must be greater than 0 less than?></body>
View Code

PHP Object-oriented encapsulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.