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

Some keywords used in PHP object-oriented

Mar 29, 2018 pm 04:33 PM
php use Keywords

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!

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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles