Home > Backend Development > PHP Tutorial > Some keywords used in PHP object-oriented

Some keywords used in PHP object-oriented

小云云
Release: 2023-03-22 17:24:02
Original
2103 people have browsed it

How to use common keywords in object-oriented PHP. This article mainly shares with you the use of some keywords in object-oriented PHP. I hope it can help everyone.

1. final: The final keyword can be added before a class or a method in a class, but final cannot be used to identify member attributes.

Function: Classes marked with final cannot be inherited.

     Member methods marked as final in a class cannot be overridden in subclasses.

Summary: final means final, so classes or member methods in classes using the final keyword cannot be changed.

2. static: The static keyword identifies the member attributes or member methods in the class as static. The member attributes identified by static belong to the entire class. Static members always exist uniquely and are used by all object instances of the class. shared.

Calling method:

Outside the class: Class name::static member attribute name; Class name::static member method name (); Object reference::static member attribute name; Object reference: :Static member method name();

It is recommended to use the class name to access static properties or methods outside the class.

Within the class: self::static member attribute name; self::static member method name ().

Tips:

When using static methods, you need to pay attention. Only static members can be accessed in static methods. Because non-static members must be accessed through a reference to the object, this is usually done using $this. Static methods can also be accessed directly using the class name when the object does not exist. Without an object, there is no $this reference. Without the $this reference, amorphous members in the class cannot be accessed. , but you can use the name or self to access static members in non-static methods

   .

 3. const: The const keyword defines constants in the class, and the define() function also defines constants, but it defines constants outside the class.

The calling method is the same as that of static members. They are accessed through the class name or using the self keyword in the member method. However, it is not recommended to use object references for access.

Notes:

The attributes of constants identified by const are read-only and cannot be reassigned, so they must be initialized when defined. Do not use the "$" symbol before a constant name declared using const, and constant names are usually capitalized

    .

 4. instanceof: The instanceof keyword determines whether an object is an instance of a class, a subclass of a class, or implements a specific interface. If so, it returns true, otherwise it returns false.

Usage: Object reference instanceof class name For example: $man instanceof People;

Note: Class names do not use delimiters (no quotation marks).

Related recommendations:

Usage of final keyword in official way in php

How to use regular expressions to block selected keywords

Detailed explanation of the usage of use keyword in php

The above is the detailed content of Some keywords used in PHP object-oriented. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template