Php Object-Oriented Programming-PHP development

Source: Internet
Author: User
Objects are the units of object-oriented programming. You can use classes to create many objects. This is also the significance of creating classes. It is easy to create an object. You only need to use the new keyword and add a method with the same name as the class name. The instantiation format of an object is as follows: $ variable name new Class Name ([parameter]) Description: "$ variable name" is the reference name of the object created by the class, it allows you to access the member attributes and member methods of an object. "New" indicates creating a new object. "Parameter" specifies the class constructor used to initialize the object value. We have known what a class is called and the class has member attributes and member methods. In this article, let's get to know the object.


ObjectIt is an object-oriented Programming Unit. Many objects can be created by examples through classes. This is also the significance of creating classes. It is easy to create an object. You only need to use the new keyword and add a method with the same name as the class name. The format of Object Instantiation is as follows:

$ Variable name = new Class Name ([parameter])

Note:

"$ Variable name" is the reference name of the object created by the class. It can be used to access the member attributes and member methods of the object.

"New" indicates creating a new object.

"Parameter" specifies the class constructor used to initialize the object value.


A Person class has been declared as follows:

 

Use this class to create objects

$ Person1 = new Person (); // creates the first Person Class Object, referencing the object named $ Person1 $ Person2 = new Person (); // creates the second Person Class Object, referencing the object named $ Person2

...

A class can create many independent objects. The above only creates two objects: $ Person1 and $ Person2, which is equivalent to opening two spaces in the memory to store these two objects.


Access of members in the object

The preceding example shows two objects $ Person1 and $ Person2. Now, grant the initial values to their member $ name respectively.

$ Person1-> name = "Zhang San"; $ Person2-> name = "Li Si;

AccessObjectMember attribute $ name in

Echo "$ The name Of The Person1 object is:". $ Person1-> name ."
; // $ The name Of The Person1 object is Zhang San echo. "$ The name Of The Person2 object is:". $ Person2-> name ."
; // $ The Name Of The Person2 object is Li Si

Access Object member method say ()

$Person1->say();$Person2->say();

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.