A newbie may ask you a related question

Source: Internet
Author: User
A beginner may ask you a question! In object-oriented PHP, how does one understand static variables and static methods? What are the differences between variables and static variables? My personal understanding is as follows: 1. Static variables can be used by all instantiated objects and are public variables. 2. Static variables can only be used by static methods,

A beginner may ask you a question! In object-oriented PHP, how does one understand static variables and static methods? What are the differences between variables and static variables? My personal understanding is as follows: 1. Static variables can be used by all instantiated objects and are public variables. 2. Static variables can only be used by static methods,

A beginner may ask you a question!
In object-oriented PHP, how does one understand static variables and static methods?
What are the differences between variables and static variables?
My personal understanding is as follows:
1. Static variables are available to all instantiated objects and are public variables.
2. The static method can only use static variables, but not general variables.
Do you understand the opposite way? In addition, what is the general situation during actual development?




------ Solution ----------------------
Static attributes are "Global" variables with limited scopes and are not limited to use in objects (as long as they are not private or protected)
The static method is a function with limited scope. The static method does not have $ this, so the object attributes guided by $ this cannot be used.

Static methods and attributes are actually chicken ribs. They are introduced with the mentality that people need to have.
Although it is not useless, you don't need it if you don't need it.
------ Solution ----------------------
Supplement the upstairs,
Static methods can be called directly without instantiation.

For example:

Class abc {
Public static function test (){
Echo 'test ';
}
}

Abc: test (); // test
?>

------ Solution ----------------------
Static attributes and static methods are shared by all objects, but they do not mean that objects can be directly used. They can be directly accessed through classes without being instantiated, it cannot be accessed through $ this within the class.

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.