Php: detailed description of object data type instances

Source: Internet
Author: User
Objects are data types that store data and information about how to process data. It is an entity used to describe objective things in the system. it is a basic unit of the system. An object consists of a group of attributes and a group of services that operate on these attributes. What is an object?

Objects are data types that store data and information about how to process data. It is an entity used to describe objective things in the system. it is a basic unit of the system. An object consists of a group of attributes and a group of services that operate on these attributes.

Syntax

In PHP, objects must be explicitly declared.

First, we must declare the class of the object. We use the keyword class to declare a class, followed by the class name, and the subject is enclosed by the {} symbol.

class class_name{    ......}

Class contains attributes and methods.

Attribute

Variables are declared by using the keyword var in the class definition, that is, the class attributes are created, also called the class member attributes.

Syntax:

class class_name{    var $var_name;}

For example, if a person's class is defined, the person's name, age, gender, and so on can be regarded as the attributes of the person class.

Method

The class method is created by declaring a function in the class definition.

Syntax:

Class class_name {function function_name (arg1, arg2 ,......) {Function code }}

Class applications

A class that defines attributes and methods is a complete class. it can contain a complete processing logic in a class. Use the new keyword to instantiate an object to apply the logic in the class. Multiple objects can be instantiated at the same time.

Syntax:

object = new class_name();

After an object is instantiated, use the-> operator to access the member attributes and methods of the object.

Syntax:

object->var_name;object->function_name;

To access the attributes or methods of a member in a defined class, you can use the pseudo variable $ this. $ This indicates the current object or the object itself.

Example:

 Name ."
"; Echo" My website is :". $ this-> age ;}// class definition ends // instantiate an object $ p1 = new Person (); // assign $ p1 object attributes $ p1-> name = "PHP 文""; $ p1-> age = 'www .php1.cn '; // call the say () in the object () method $ p1-> say ();?>

Run this example and output:

The above is a simple example of "object" of the composite data type. For more information about objects, visit ourTarget topic,In the next section, we will explain the "resources" of two special data types in PHP"

The above is php: details about object data type instances. 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.