PHP Object-oriented programming (i), PHP object-oriented programming (_php tutorial

Source: Internet
Author: User

PHP Object-oriented programming (a), PHP object-oriented programming (


Class-To-object relationships:

The class is like a human colony. We instantiate an object from a class as if it were a person.

Object-oriented programs are objects, but objects are instantiated through classes, so the first thing we need to do is to declare classes and make a class easy.

The format of the class

Class Name {}

Class can be understood as a group of people, if we want to introduce this person to others so

First, you'll introduce the person's name, gender, age, height, weight, phone, home address, and so on.

Then, you want to introduce what this person can do, can drive, speak English, can use computers and so on.

From the perspective of definition can be divided into:

1. Static description such as: person's name, gender, age, height, weight, telephone, home address and so on we refer to static descriptions as member properties

We can use Var to define such as Var $description; At this point we should note that the declaration is not necessary to assign a value , such as the name of the person is xiaoming may find in this crowd dozens of Xiao Ming

2. Dynamic description such as: This person can drive, can speak English, can use the computer and so on we will describe the dynamic description as a member method

Class Person {member properties: Name, gender, age, height, weight, phone, home Address member method: can drive, speak English, can use the computer}
 
  PHPclass  person{    //  Below is a member of the human attribute    var$name;    // the name    of the man var $sex;    // man's sex    var $age;    // person's age    //Below is the method    of the person's membership function // The way this man can talk.     {         echo "This person is talking";    }      function // The way this man can walk.     {        echo "This person is walking";    }}? >

When a class is defined, we need to instantiate that class before we can use it.

Use the New keyword $ object name = new class name ();

 
  

Now that we've got the sample class ready we're going to learn how to use members in a class

Object, Properties $p 1->name; $p 2->age; $p 3->sex;

Object, Method $p 1->say (); $p 2->run ()

 Name = "Zhang San"; $p 1->sex = "male"; $p 1->age = 20;//The following three lines are the properties of the Access $p1 object echo "The name of the P1 object is:". $p 1->name;echo "The gender of the P1 object is:". $p 1->sex;echo "The age of the P1 object is:". $p 1->age;//The following two lines to access the methods in the $p1 object $p1->say (); $p 1->run ();//The following three lines are assigned $P2 = "$p2->name" to the property of the; $p John Doe = "female"; P2->age = 30;//The following three lines are the properties that access the $p2 object echo "The name of the P2 object is:". $p 2->name;echo "The gender of the P2 object is:". $p 2->sex;echo "The Age of the P2 object is:". $p 2->age;//The following two lines to access the methods in the $p2 object $p2->say (); $p 2->run ();//The following three lines are assigned $P3 "$p3->name=" to the property of the; $p Harry "Male"; p3->age=40;//The following three lines are the properties that access the $p3 object echo "The name of the P3 object is:". $p 3->name;echo "The gender of the P3 object is:". $p 3->sex;echo "The age of the P3 object is:". $p 3->age;//The following two lines to access the methods in the $p3 object $p3->say (); $p 3->run (); >

At this time, we have basically been able to use the class, but we found that there is still a little bit is we can access outside, but not in-house access we introduce a concept of "this" usage if the internal access can be assigned to the value of our code will be reduced

  

 
  Name. "Gender:". $this->sex. "My Age is:". $this->age;} function run ()//This person can walk the way {echo "This person is walking";}} $p 1 = new person (); Create Instance Object $P1$P2 = new Person (); Create Instance Object $p2$p3 = new Person (); Create an Instance object $p3//the following three lines are assigned a value to the $p1 object property $p1->name = "Zhang San"; $p 1->sex = "male"; $p 1->age = 20;//below Access $p1 object of speech method $p1->say ();/ The following three lines are assigned to the $p2 object property $p2->name = "John Doe", $p 2->sex = "female", $p 2->age = 30;//below to access $p2 object's Speech method $p2->say ();//The following three lines are for $ P3 object property Assignment $p3->name = "Harry"; $p 3->sex = "male"; $p 3->age = 40;//below Access $p3 Object Speaking method $p3->say ();? >

Analyze

function say ()//This person can speak the way {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age;}

In the above code, $this refers to the object to which the value is assigned.

Construction method __construct () and the destructor __destruct ()

Construction Method __construct (): The method of automatically using new to instantiate an object (which can be understood to construct a bridge for passing parameters when the object is instantiated ). Understood as a queue

  

 
  The name assigns the value $this->name = $name;//The $sex given to the member property $this->sex by the constructor method gives the value $this->sex = $sex;//The $age passed through the constructor method to the member property $ This->age the value $this->age = $age;} This person's way of speaking function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age;}} The construction method creates 3 objects $p1, P2, $p 3, each passing in three different arguments for name, gender, and age $p1 = new Person ("Zhang San", "male"), $p 2 = new Person ("John Doe", "female", "3"), $p = new Pers On ("Harry", "male", 40);//The following access to the $p1 object in the speech method $p1->say ();//The following access to the $p2 object in the speech method $p2->say ();//The following Access $p3 object in the speech method $p3->say (); >

 

The output is:

My name is called: Zhang San Sex: Male my age is: 20 my name is called: John Doe Sex: Female my age is: 30 My name is called: Harry Sex: Male My age is: 40

 

destructor __destruct (): destructors allow some columns to be manipulated before destroying a class the destructor of a class must be __destruct () Note that the destructor cannot come with any arguments understood as a stack

  

 
  The name assigns the value $this->name = $name;//The $sex given to the member property $this->sex by the constructor method gives the value $this->sex = $sex;//The $age passed through the constructor method to the member property $ This->age the value $this->age = $age;} This person's way of speaking function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age;} This is a destructor that calls function __destruct () {echo "Goodbye" before the object is destroyed. $this->name;}} The construction method creates 3 objects $p1, P2, $p 3, each passing in three different arguments for name, gender, and age $p1 = new Person ("Zhang San", "male"), $p 2 = new Person ("John Doe", "female", "3"), $p = new Pe Rson ("Harry", "male", 40);//The following access to the $p1 object in the speech method $p1->say ();//The following Access $p2 object in the speech method $p2->say ();//Access $p3 object in the following way $p3->say () ;? >

  

The output is:

My name is called: Zhang San Sex: Male my age is: 20 my name is called: John Doe Sex: Female my age is: 30 My name is called: Harry Sex: Male My age is: 40
Bye, Harry.
Bye, John Doe.
Bye, Zhang San.

http://www.bkjia.com/PHPjc/1101503.html www.bkjia.com true http://www.bkjia.com/PHPjc/1101503.html techarticle PHP Object-oriented programming (i), PHP object-oriented programming (class-to-object relationships: Classes are like a human group we instantiate an object from a class as if it were a person. To face to the ...

  • 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.