Home Backend Development PHP Tutorial Section 7 - Static members of classes - Classes and Objects in PHP5 [7]_PHP Tutorial

Section 7 - Static members of classes - Classes and Objects in PHP5 [7]_PHP Tutorial

Jul 20, 2016 am 11:03 AM
and php5 member of kind static

Section 7 - Static members of a class
Static members of a class are different from ordinary class members: static members have nothing to do with the instance of the object, only the class itself. They are used to implement the functions and data that the class wants to encapsulate, but Excludes the functionality and data of a specific object. Static members include static methods and static properties.
Static properties contain data to be encapsulated in a class and can be shared by all instances of the class. In fact, in addition to belonging to a fixed class and In addition to restricting access, the static properties of a class are very similar to the global variables of a function
We use a static property Counter::$count in the following example. It belongs to the Counter class and does not belong to any Counter instance. You cannot use this to refer to it, but you can use self or other valid naming expressions. In the example, the getCount method returns self::$count, not Counter::$count.
The static method implements the functions that the class needs to encapsulate, It has nothing to do with a specific object. Static methods are very similar to global functions. Static methods can fully access the properties of the class, or they can be accessed by instances of the object, regardless of the access qualifier.
In example 6.3, getCount is an ordinary method, called with ->. PHP creates a this variable, although the method is not used. However, getCount does not belong to any object. In some cases, we even want to call it when there is no valid object , then static methods should be used. PHP will not create this variable inside static methods, even if you call them from an object.
Example 6.7 is derived from 6.3 changing getCount to a static method. The Static keyword cannot prevent an instance from using -> operator to call getCount, but PHP will not create this variable inside the method. If you use this-> to call, an error will occur.
//6.3 Example refers to Section 4 - Constructor and Analysis The example in the constructor (see the previous article), by comparing the two examples, you can easily grasp the difference between
//static method and ordinary method.
You can write a method to determine whether this is established to show whether it is called statically or non-statically. Of course, if you use the static keyword, this method will always be static no matter how it is called.
Your class can also define constant attributes, no need To use public static, just use the const keyword. Constant properties are always static. They are properties of the class, not the properties of the object that instantiates the class.
Listing 6.7 Static members
class Counter
{
private static $count = 0;
const VERSION = 2.0;
function __construct()
{
self: :$count++;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445275.htmlTechArticleSection 7 - Static members of classes Static members of classes are different from ordinary class members: Static members and objects The instances have nothing to do with, only the class itself. They are used to implement the functions and data to be encapsulated by the class...
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

What is the difference between php5 and php8 What is the difference between php5 and php8 Sep 25, 2023 pm 01:34 PM

The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

How to set up the keyboard boot function on a GIGABYTE motherboard (enable keyboard boot mode on GIGABYTE motherboard) How to set up the keyboard boot function on a GIGABYTE motherboard (enable keyboard boot mode on GIGABYTE motherboard) Dec 31, 2023 pm 05:15 PM

How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

The first choice for CS players: recommended computer configuration The first choice for CS players: recommended computer configuration Jan 02, 2024 pm 04:26 PM

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

Digital audio output interface on the motherboard-SPDIF OUT Digital audio output interface on the motherboard-SPDIF OUT Jan 14, 2024 pm 04:42 PM

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

Glodon Software's computer configuration recommendations; Glodon Software's computer configuration requirements Glodon Software's computer configuration recommendations; Glodon Software's computer configuration requirements Jan 01, 2024 pm 12:52 PM

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

Naming conventions in PHP: How to use camel case naming for classes, methods and variables Naming conventions in PHP: How to use camel case naming for classes, methods and variables Jul 30, 2023 pm 02:43 PM

Naming conventions in PHP: How to use camelCase notation to name classes, methods, and variables In PHP programming, good naming conventions are an important coding practice. It improves code readability and maintainability, and makes teamwork smoother. In this article, we will explore a common naming convention: camelCase and provide some examples of how to use it in PHP to name classes, methods, and variables. 1. What is camel case nomenclature? CamelCase is a common naming convention in which the first letter of each word is capitalized,

PHP error: Unable to declare class repeatedly, solution! PHP error: Unable to declare class repeatedly, solution! Aug 25, 2023 pm 04:13 PM

PHP error: Unable to declare class repeatedly, solution! It is common for developers to encounter problems. In PHP development, we often encounter a common error: the class cannot be declared repeatedly. This problem seems simple, but if not solved in time, the code will not execute correctly. This article will introduce the cause of this problem and provide a solution for your reference. When we define a class in PHP code, if the same class is defined multiple times in the same file or multiple files, an error that the class cannot be declared repeatedly will occur. This is

In-depth analysis of the role and usage of the static keyword in C language In-depth analysis of the role and usage of the static keyword in C language Feb 20, 2024 pm 04:30 PM

In-depth analysis of the role and usage of the static keyword in C language. In C language, static is a very important keyword, which can be used in the definition of functions, variables and data types. Using the static keyword can change the link attributes, scope and life cycle of the object. Let’s analyze the role and usage of the static keyword in C language in detail. Static variables and functions: Variables defined using the static keyword inside a function are called static variables, which have a global life cycle

See all articles