Home  >  Article  >  Backend Development  >  Object-oriented keywords in PHP, php object-oriented keywords_PHP tutorial

Object-oriented keywords in PHP, php object-oriented keywords_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:491437browse

Object-oriented keywords in PHP, php object-oriented keywords

Commonly used keywords in php object-oriented include final, static, const

(1) final:
1. Final cannot modify member attributes
2. Final can only modify classes and methods

Function:
Classes modified with final cannot be inherited by subclasses
Methods modified with final cannot be overridden by subclasses
Used to restrict classes from being inherited. If methods cannot be overridden, use final
(2.) static:
1. Use static to modify member attributes and member methods, but not classes
2. Member attributes modified with static can be shared by all objects of the same class
3 , Static data is stored in the data segment in the memory (initializing the static segment)
4. Static data is allocated to the memory every time the class is loaded. When the class is used in the future, it is directly obtained from the data segment. Get
5. As long as this class is used in the program (this class name appears), the class is loaded

Note: Static members must be accessed using class names. There is no need to create objects or objects to access
Class name::static members
If you use static members in a class, you can use self to represent this class. (Function equivalent to $this)
self:: Static member
6. Static methods cannot access non-static members. In non-static methods, static members can be accessed.
This is because non-static members must be accessed using objects. To access internal members, $this is used. Static methods do not need to be called using objects, so there is no object, and $this cannot represent any object. Non-static members must also use objects. If you are sure that a method does not use non-static members, you can declare this method as a static method (objects cannot be created and accessed directly using the class name)
(3.) const:
1, it can only Modify member attributes
2. Use const
to declare constant attributes in a class. 3. The naming method has the same effect as define
4. The access method is the same as static static member attributes: class name::constant self ::Constant
5. Constants must be given initial values ​​when declared
6. Constants cannot be reassigned after declaration

Object-oriented in PHP , When is the final keyword used?

Usually to prevent a method of the parent class from being overridden.

The difference between public and var in PHP object-oriented

The functions of public and var are similar, because if the variables defined by var are not protected or private, they default to public

. In php4, var
is generally used, and in php5, public is generally used.

Nowadays, public is basically used instead of var
var is used to define variables; public is used to define the visibility of property (attribute) and method (method)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/908126.htmlTechArticleObject-oriented keywords in PHP, php object-oriented keywords Commonly used keywords in php object-oriented include final, static, const (1) final: 1, final cannot modify member attributes 2, final can only modify...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn