Php class definition and instantiation method

Source: Internet
Author: User
Class is the definition of an object. It contains information about the object action method, including its name, method, attribute, and event. In fact, it is not an object because it does not exist in the memory. When the code of the referenced class is run, a new instance of the class, that is, an object, is created in the memory. Although there is only one class, multiple objects of the same type can be created from this class in the memory. The class is defined by the class keyword. Php class definition

Class is the definition of an object. It contains information about the object action method, including its name, method, attribute, and event. In fact, it is not an object because it does not exist in the memory. When the code of the referenced class is run, a new instance of the class, that is, an object, is created in the memory. Although there is only one class, multiple objects of the same type can be created from this class in the memory. The class is defined by the class keyword.

Basic syntax:

Class name {// attributes, methods}

Note:

1. define a class (class only)

2. define the class attribute public (public is the modifier, there are three in total, public, protected, private, here we only use public)

For example:

Defines the color and price of a car.

Class car {public $ color; // defines the attribute public $ price ;}

Php class instantiation method

Class instantiation is also called creating an object, instantiating an object, or instantiating a class.

Here is a simple example:

We define a person's class and instantiate this class.

Class Preson {// defines a Preson class public $ name; // defines the class attributes (name, gender, age, etc.) public $ age; public $ gender; // public .....} // new indicates creating a new person and assigning the new object to $ Preson1. this is the instantiation of $ Preson1 = new Preson (); // instantiate the class $ Preson1-> name = "zhang san"; $ Preson1-> age = 22; $ Preson1-> gender = "female"; echo $ Preson1-> name. ''. $ Preson1-> age. ''. $ Preson1-> gender; // If you want to output the second instance, the output is OK. you only need to change the variable name $ Preson2 = new Preson (); // instantiate the class $ Preson2-> name = "Xiao Liang"; $ Preson2-> age = 25; $ Preson2-> gender = "male ";

This section describes the basic concepts and simple instantiation of classes and objects.

The above is the definition and instantiation method of php classes. For more information, see other related articles in the first PHP community!

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.