52nd Day Class object oriented

Source: Internet
Author: User

class and object concepts anything can be called an object, referring to specific information

A class is a commonality that abstracts out all the same objects.

Defining and Using Classes

/*The access modifier public, which is publicly available, can be called anywhere, the default entry protected protected, and can only be accessed within the class or class's subclass, only within the class.*///Defining ClassesclassRen//class name, first letter general capitalization{     Public $Name;//member Variables    protected $age; Private $height; var $sex;//No access modifier preceded by Var, default is public    function__construct ($a)//constructor, function name fixed cannot be changed    {        $this->sex=$a; }      function__destruct ()//destructors, automatic execution of object destruction, fixed function name cannot be changed    {        Echo"The object has been destroyed."; }    functionRun ()//member Methods    {        Echo"This man is running <br>"; }    functionSay () {Echo $this->name. " is talking ";//$this Reference the data inside the class, $this which object calls which object is represented    }}//using Classes$a=Newren ("male");//Initializing Objects$a->name = "Zhang San";//Assigning a value to an object member variableVar_dump($a);//All information about the output object$a->run ();//Call member Method//this reference$a-Say ();Echo"<br>";//constructor FunctionEcho $a->sex;

encapsulates one of the three main features of object-oriented (encapsulation, inheritance, polymorphism)

//encapsulation for the purpose of making the class more secure, without direct access to the outside world. The member variable is changed to private, and the indirect operation of the member variable is implemented by various methods. classRen {Private $name; Private $age; Private $height; Private $sex; functionSetage ($a)//functions for assigning class private variables    {        if($a>=10&&$a<=50)            $this->age=$a; }    functionGetage ()//functions that return class-private variables    {        return $this-Age ; }    function__set ($name,$value)//The magic method of assigning a value to a private variable inside a class, the function name cannot be changed    {        if($name= = "Age")        {            if($value>20&&$value<100)                $this-$name=$value; }        Else            $this-$name=$value; }    function__get ($name)//Gets the Magic method returned by the private variable inside the class, the function name cannot be changed    {        return $this-$name; }}$a=NewRen;$a->setage (50);Echo $a-getage ();$a->name = "Zhang San"; $a->age = "40";//executing the statement automatically calls the __set () method$a->sex = ' Male ';Var_dump($a);Echo"<br>";Echo $a->name;//executing the statement automatically calls the __get () method

52nd Day Class object oriented

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.