Php -- oop notes (1), php -- oop notes (

Source: Internet
Author: User

Php -- oop notes (1), php -- oop notes (
// Object-oriented model concept: In PHP5, it acts as a class and defines a model using the class keyword.
/*
Class definition involves the following:
1. Class Members (attributes) [attributes of a person: name, age]
2. Definition of class attributes (variables): Use the keyword var
3. The constant of the class will never change the attribute [Person's constant: lineage, gender]
4. How to access members
A) Class instantiation (new)
B) usage of class pointers (Symbol:->)
C) constants of the constant class (constant pointer symbol class name: constant name)


Define the class method: Use the function keyword
1) A class method represents a behavior [a person's behavior: Eat, walk, say]
2) how to access the class (Symbol:->)
3) The behavior is diverse. You can use the function say (parameter) parameter to define the behavior)
4) returned behavior values:
Note: There is an odd problem (odd problem) echo & echo in php5. This odd problem first has a priority level. If it is a class method, it will execute everything else, if it is a function method, it will be lower than the class method, but higher than the echo itself. The best way to solve this problem is to use the return keyword.


Access Permissions for member attributes:
1) public (Keyword: public, var) ---- highest level (can be inherited and can be accessed at Will)
2) private (Keyword: private) --- lowest level (it cannot be inherited or accessed externally)
3) protected (Keyword: protected) --- Medium level (can be inherited, cannot be accessed outside)


Method access permission
1) public (Keyword: public, which is the default method and can be omitted without writing) ---- highest level (can be inherited and can be accessed at Will)
2) private (Keyword: private) --- lowest level (it cannot be inherited or accessed externally)
3) protected (Keyword: protected) --- Medium level (can be inherited, cannot be accessed outside)


Static attributes (static variables): They act the same as class constants and can be changed (but may not change for a long time, or even never change). They can be understood as static variables, terminology: static variable terminology
For example, if I am a Chinese, even if I marry a Hong Kong wife and my child is born, his skin color will never change. But can the color change forever? There is another possibility that my son may marry a Russian woman in the future. The grandson will not be yellow skin of Chinese people, and his hair may not be black hair.
Declare static keywords (static ):
1) how to access static variables (Class Name: $ variable name)
2) What is the difference between a class constant and a static variable?
Answer: (1) static variables can be changed, and static constants cannot be changed. (2) The syntax stipulates that constants cannot be assigned externally, and constants can only be accessed externally. (3) constants must be initialized when defined in classes and interfaces.


Static Methods: The functions and problems are the same as static variables, but one is a variable and the other is a behavior.
1) how to access (Class Name: Method Name ())


Xx interview question: static variables and static methods, how can class constants be accessed inside the class?
Answer: (1) You can access static variables (you can use Class Name: $ variable name or self: $ variable name)
(2) Static access methods can be used (Class Name: method name or self: method name can be used)
(3) constants can be used (you can use Class Name: constant name or self: constant name)
(4) self can only be used for internal access to static variables or methods, constants, and external use of class names only


Xx interview: How to declare static attributes inside external access?
Answer: define a public method for internal access to private variables.


Permission for static variables and static methods
1) public (Keyword: public, which is the default method and can be omitted without writing) ---- highest level (can be inherited and can be accessed at Will)
2) private (Keyword: private) --- lowest level (it cannot be inherited or accessed externally)
3) protected (Keyword: protected) --- Medium level (can be inherited, cannot be accessed outside)

*/

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.