Php object-oriented? Static Member

Source: Internet
Author: User
Php object-oriented? Static Member Php object-oriented? Static Member

Static attributes

Attributes declared using the static keyword

This static property is logically defined in the class. Ensure that a class corresponds to an attribute.

Example:

Class Student

{

Public $ stu_id;

Public $ stu_name;

Public static $ stu_count = 0;

Public function _ constuct ($ id, $ name)

{

$ This-> stu_id = $ id;

$ This-> stu_name = $ name;

}
}

Access static attributes:

Access through the class: in the use of static access symbols (::)

Class: Member

Example:

Class Student

{

Public $ stu_id;

Public $ stu_name;

Public static $ stu_count = 0;

Public function _ constuct ($ id, $ name)

{

$ This-> stu_id = $ id;

$ This-> stu_name = $ name;

Student: $ stu_count ++;

}
}

: The access is called static access.-> Non-static access (object access)

When you access static attributes, if you access them within the class: you can use the current class of self.

Example:

Class Student

{

Public $ stu_id;

Public $ stu_name;

Public static $ stu_count = 0;

Public function _ constuct ($ id, $ name)

{

$ This-> stu_id = $ id;

$ This-> stu_name = $ name;

Self: $ stu_count ++;

}
}

Note: the difference between $ this and self

$ This indicates this object, and self indicates this class.

Static method

The logic meaning of static methods is also a method that defines the class. Similarly, the call form class ::

Example:

Class Student

{

Public $ stu_id;

Public $ stu_name;

Public static $ stu_count = 0;

Public function _ constuct ($ id, $ name)

{

$ This-> stu_id = $ id;

$ This-> stu_name = $ name;

Self: $ stu_count ++;

}

Public static function sayCount ()

{

Echo "run static ";

}
}

Student: sayCount ();

The main difference between static and non-static methods is whether an object's execution environment can be received.

Is it possible to assign a value to $ this in the method?

Only when an object calls a method can the object execution environment be passed to the method, and $ this can be assigned a value.

Non-static attributes cannot be used in static methods.

One static method can only process static data (static attributes)

Example:

Class Student

{

Public $ stu_id;

Public $ stu_name;

Public static $ stu_count = 0;

Public function _ constuct ($ id, $ name)

{

$ This-> stu_id = $ id;

$ This-> stu_name = $ name;

Self: $ stu_count ++;

}

Public static function sayCount ()

{

Echo self: $ stu_count;

}
}

Student: sayCount ();

Special method access problems in Php:

Emphasis: static methods should be called by class, non-static methods should be called by objects

Class C

{

Public function f1 ()

{

Echo 'F1
';

}

Public static function f2 ()

{

Echo 'F2
';

}

}

$ O = new C;

$ O-> f1 ();

C: f2 ();

However:

1. both static and non-static methods can be accessed using classes: However, if a class calls a non-static method statically, an error with nonstandard syntax is reported.

2. The difference lies in $ this.

You can use $ this in the method only when you call a non-static method using an object.

Class C

{

Public function f1 ()

{

Echo 'F1
';

}

Public static function f2 ()

{

Echo 'F2
';

}

}

C: f1 ();

C: f2 ();

Summary: the static and non-static methods of the class are saved in only one copy, but the runtime environment is different. You can use $ this in the method only when you call a non-static method using an object.

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.