Common keywords used in PHP object-oriented (final, static, const, and instanceof)

Source: Internet
Author: User

The use of common keywords in PHP object-oriented:

1. The Final:final keyword can be added before a method in a class or class, but the final identity member property cannot be used.

Function: A class that uses the final identity and cannot be inherited.

A member method that uses the final identity in a class and cannot be overridden in a subclass.

Summary: Final is meant to be final, so a class that uses the final keyword or a member method in a class cannot be changed.

2. The Static:static keyword identifies a member property or member method in a class as static, the member property of the static identity belongs to the entire class, and the static member is always unique, shared by all the object instances of the class.

Call Mode:

Outside class: Class Name:: Static member property name; Class Name:: Static member Method name (); object reference:: static member property name; object reference:: Static member Method name ();

It is recommended to use the class name outside the class to access a static property or method.

In class: Self:: static member property name; Self:: Static member Method name ().

Tips:

When using static methods, it is important to note that only static members can be accessed in static methods. Because non-static members must be accessed through a reference to an object, it is usually done using $this. The static method in the object does not

It can also be accessed directly using the class name if there is no object, no $this reference, no $this reference, no access to the non-crystal members in the class, but you can use the grams name or self in a non-static method

Access static members.

3. The Const:const keyword defines a constant in a class, and the Define () function also defines a constant, but it defines a constant outside of the class.

The invocation is the same as a static member, which is accessed through the class name or by using the Self keyword in the member method, but is not recommended for access using object references.

Precautions:

Const-identified constants are read-only and cannot be re-assigned, so be sure to initialize them at the time of definition. Do not use the "$" symbol before using const-declared constants, and the constant name is usually uppercase

Of

4. The Instanceof:instanceof keyword determines whether an object is an instance of a class, a subclass of a class, or implements a particular interface, or returns True if it is false.

Usage: Object reference instanceof Class name for example: $man instanceof people;

Note: The class name does not use delimiters (quotation marks are not used).

Common keywords used in PHP object-oriented (final, static, const, and instanceof)

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.