Common and static properties in PHP

Source: Internet
Author: User

Normal Properties (instance properties):

the word for the instance is:instance

examples, in fact, also called "Object";

A normal (instance) attribute is a property that can be used on an object that is instantiated from that class!

definition form:

class name {

var $ Property name = initial value;

var $ property name; // This property has no initial value;

the above var can also be replaced with public , such as:

Public $ Property name = initial value;

Public $ Property name; // This property has no initial value;

}

form of Use:

Is through the object of the class to use the normal attribute (instance property):

$ Object , property name;

Because the nature of a property is a variable, it can be viewed and used as a variable, such as:

Property name $v 1 = $ Object ;

echo $ Object , property name;

$v 2 = $ Object , property name * 3 + 5; // calculation

Static properties:

A static property, which is essentially a "variable", has one characteristic: the variable is only subordinate to the "class", i.e.:

A static property in a class , only "One piece of data";

But:

An instance attribute in a class can have "multiple data"--each object is created with a copy of the data;

definition form:

class name {

Static $ Property name = initial value;

Static $ property name; // This property has no initial value;

}

form of Use:

use the class name and Range Resolver (::) to manipulate the static properties:

class Name:: $ static property name; // Note: property name with $ symbol

contrast 1:

Use of constants: Class Name:: Constant name;

contrast 2:

use of Instance properties: Object Name , instance property name; // Note: Property name does not have a $ symbol

The result is:

Visible:

1, the instance property, is each object can be different data, is also each object "own" the data;

2, static property, which he does not belong to any object, but belongs to the class itself, but also can be understood as a common data for all objects;

Common and static properties in PHP

Related Article

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.