Home >Backend Development >PHP Tutorial >Common keywords such as PHP object-oriented development_PHP tutorial
Commonly used keywords for classes in PHP include: lock (fianl), somewhat similar to this keyword (self), static attribute (static), and constant keyword (const). Let me sort it out for you.
Common keywords in the class
1.fianl: lock
2.self: somewhat similar to this keyword
3.static: static attribute
4.const: constant keyword
1. Keyword: fianl
An important keyword used to define classes and methods. When defining a class, the class cannot be inherited. When used to define a method, the method cannot be overloaded.
1. Final cannot modify member attributes (this keyword is often not used in classes)
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 and methods from being overridden, use fianl
Examples of classes using the final keyword:
final class Person
{
…
}
When a class defined by final is inherited, the following error will be prompted:
Fatal error: Class Student may not inherit from final class (Person) in ...
Example of method using final keyword:
class Person
{
final function say()
{
… …
}
}
Example #1 Final method example
The code is as follows | Copy code | ||||||||||||||||||||
2. Keyword: self When accessing member variables or methods in a PHP class, if the referenced variable or method is declared as const (defining constant) or static (declaring static), then you must use the operator::, otherwise if it is referenced The variable or method is not declared as const or static, then the operator -> must be used. In addition, if you access a const or static variable or method from within the class, you must use self-reference. On the contrary, if you access a non-const or static variable or method from within the class, you must use self-reference. $this
self::Class internal members (properties or methods) Note: Because it is meaningless to access internal properties or methods without a mathematical class, self is generally used to access: static members, constants, and other defined content in the class.
3. Keyword: static Memory optimization, used to define static properties or methods, which can be used when the class has not been instantiated. Static properties occupy memory alone and do not repeatedly occupy memory by creating multiple objects. Format: Access to static members (inside the class): Access to static members (outside the class):
is used to define constants in a class and can only modify member attributes in the class. class class1 { //It is recommended to use uppercase definitions and do not use the $ symbol. Look at a small example of PHP const:
const CHARSET="China"; 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 Previous article:PHP two-dimensional array is grouped and added by a certain key_PHP tutorialNext article:PHP two-dimensional array is grouped and added by a certain key_PHP tutorial Related articlesSee more |