


PHP object-oriented guide to using the finalstaticconst keyword
14. Application of the final keyword
This keyword can only be used to define classes and methods. The final keyword cannot be used to define member properties, because
final means constant, we are The define() function is used to define constants in PHP, so final cannot be used to
define member properties.
Classes marked with the final key cannot be inherited;
Code snippet
final class Person{
… …
}
class Student extends Person{
}
Yes The following error occurs:
Fatal error: Class Student may not inherit from final class (Person)
Methods marked with the final key cannot be overridden by subclasses and are the final version;
Code fragment
class Person{
final function say() {
}
}
class Student extends Person{
function say() {
}
}
The following error will occur:
Fatal error: Cannot override final method Person::say()
15. Use of static and const keywords
Static keyword describes member properties and member methods in the class as static; static members What are the benefits?
Earlier we declared the "Person" human being. In the "Person" class, if we add an attribute of "the country to which the person belongs"
, we can use the "Person" class to instantiate hundreds or more There are more instance objects, and each object will
have the attribute of "country to which it belongs." If the project is developed for the Chinese, then each object will
have a country attribute. The attribute is "China" and other attributes are different. If we make the "country" attribute a static
member, then there will be only one country attribute in the memory, and there will be hundreds or more Objects share this attribute.
static members can restrict external access, because static members belong to the class and do not belong to any object instance. They are
space allocated when the class is loaded for the first time. Other classes are inaccessible and are only shared with instances of the class, which can protect the members of the class to a certain extent;
Let’s analyze it from the perspective of memory. The memory is logically divided into four segments. The object is placed in the "heap memory"
, the reference of the object is placed in the "stack memory", and the static members are placed in the "initialized static segment", when the class is
loaded for the first time When placed, it can be shared by every object in the heap memory, as shown below; The static variables of the
code segment
<? class Person{ //下面是人的静态成员属性 public static $myCountry="中国"; // var $name; //人的名子 //这是人的静态成员方法 public static function say(){ echo "我是中国人<br>"; } } //输出静态属性 echo Person::$myCountry; //访问静态方法 Person::say(); //重新给静态属性赋值 Person::$myCountry="美国"; echo Person::$myCountry; ?>
Because static members are created when the class is first loaded, static members can be accessed using the class name without the need for objects outside the class; as mentioned above, static members are used by this class Shared by each instance object, can we use the object to access the static members in the class? From the picture above, we can see that static members do not exist inside each object, but each object can be shared, so if we use an object to access members, there will be no such attribute definition, and the object cannot be accessed. Static members. In other object-oriented languages, such as Java, you can use objects to access static members. If you can use objects to access static members in PHP, we should try not to use them because we use static members. When working on a project, the purpose is to use the class name to access. Static methods in a class can only access the static attributes of the class. Static methods in the class cannot access non-static members of the class. The reason is very simple. We want to access other members of this class in the methods of this class. , we need to use the reference $this, and the reference pointer $this represents the object that calls this method. We said that static methods are not called with objects, but are accessed using names, so there is no object at all, and There is no $this reference. Without the $this reference, non-static members in the class cannot be accessed. And because the static members in the class can be accessed without objects, the static methods in the class can only access the class. Static attributes, since $this does not exist, we use a special class "self" to access other static members in a static method; self is similar to $this, except that self represents the class in which this static method is located. So in a static method, you can use the "class name" of the class where the method is located, or you can use "self" to access other static members. If there are no special circumstances, we usually use the latter, that is, "self:: member" properties" way.
Code snippet
<? class Person{ //下面是人的静态成员属性 public static $myCountry="中国"; //这是人的静态成员方法, 通过self访问其它静态成员 public static function say(){ echo "我是".self::$myCountry."<br>"; } } //访问静态方法 Person::say(); ?>
Can you access static members in non-static methods? Of course it is possible, but you cannot use "$this". References must also use class names or "self:: member attribute form".
const is a keyword for defining constants. To define constants in PHP, you use the "define()" function, but to define constants in a class, you use the "const" keyword, which is similar to that in C. #define If
its value is changed in the program, an error will occur. The member attributes modified with "const" are accessed in the same way as the members modified with "static", and the "class name" is also used. Use the "self" keyword inside the method. But you don't need to use the "$" symbol, and you can't use objects to access it.
Code snippet
<?php class MyClass{ //定义一个常量constant const constant = 'constant value'; function showConstant() { echo self::constant . "\n"; //使用self访问,不要加”$” } } echo MyClass::constant . "\n"; //使用类名来访问,也不加”$” $class = new MyClass(); $class->showConstant(); // echo $class::constant; 是不允许的
For more PHP object-oriented guide and related articles on the use of finalstaticconst keyword, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
