Home php教程 php手册 php面向对象全攻略 (十) final static const关键字的使用_php基础

php面向对象全攻略 (十) final static const关键字的使用_php基础

May 17, 2016 am 09:02 AM
const final static

14.final 关键字的应用
这个关键字只能用来定义类和定义方法,不能使用final 这个关键字来定义成员属性,因
为final 是常量的意思,我们在PHP 里定义常量使用的是define()函数,所以不能使用final 来
定义成员属性。
使用final 关键标记的类不能被继承;
代码片段
final class Person{
… …
}
class Student extends Person{
}
会出现下面错误:
Fatal error: Class Student may not inherit from final class (Person)
使用final 关键标记的方法不能被子类覆盖,是最终版本;
代码片段
class Person{
final function say() {
}
}
class Student extends Person{
function say() {
}
}
会出现下面错误:
Fatal error: Cannot override final method Person::say()
15.static 和const 关键字的使用
Static 关键字是在类中描述成员属性和成员方法是静态的;静态的成员好处在那里呢?
前面我们声明了“Person”的人类,在“Person”这个类里如果我们加上一个“人所属国家”
的属性,这样用“Person”这个类实例化出几百个或者更多个实例对象,每个对象里面就都
有“所属国家”的属性了,如果开发的项目就是为中国人而开发的,那么每个对象里面就都
有一个国家的属性是“中国”其它的属性是不同的,如果我们把“国家”的属性做成静态的
成员,这样国家的属性在内存中就只有一个,而让这几百个或更多的对象共用这一个属性,
static 成员能够限制外部的访问,因为static 的成员是属于类的,是不属于任何对象实例,是
在类第一次被加载的时候分配的空间,其他类是无法访问的,只对类的实例共享,能一定程
度对类该成员形成保护;
从内存的角度我们来分析一下,内存从逻辑上被分为四段,其中对象是放在“堆内存”里
面,对象的引用被放到了“栈内存”里,而静态成员则放到了“初始化静态段”,在类第一次
被加载的时候放入的,可以让堆内存里面的每个对象所共享,如下图;
'700')this. style="max-width:90%";" border="0" alt="php面向对象全攻略 (十) final static const关键字的使用_php基础" > 
类的静态变量,非常类似全局变量,能够被所有类的实例共享,类的静态方法也是一样
的,类似于全局函数。
代码片段

复制代码 代码如下:


class Person{
//下面是人的静态成员属性
public static $myCountry="中国";
// var $name; //人的名子
//这是人的静态成员方法
public static function say(){
echo "我是中国人
";
}
}
//输出静态属性
echo Person::$myCountry;
//访问静态方法
Person::say();
//重新给静态属性赋值
Person::$myCountry="美国";
echo Person::$myCountry;
?>

因为静态成员是在类第一次加载的时候就创建的,所以在类的外部不需要对象而使用类
名就可以访问的到静态的成员;上面说过,静态成员被这个类的每个实例对象所共享,那么
我们使用对象可不可以访问类中的静态成员呢?从上图中我们可以看到,静态的成员不是在
每个对象内部存在的,但是每个对象都可以共享,所以我们如果使用对象访问成员的话就会
出现没有这个属性定义,使用对象访问不到静态成员的,在其它的面向对象的语言中,比如
Java 是可以使用对象的方式访问静态成员的,如果PHP 中可以使用对象访问静态成员的话,
我们也尽量不要去使用,因为静态的成员我们在做项目的时候目的就是使用类名去访问。
类里面的静态方法只能访问类的静态的属性,在类里面的静态方法是不能访问类的非静
态成员的,原因很简单,我们要想在本类的方法中访问本类的其它成员,我们需要使用$this
这个引用,而$this 这个引用指针是代表调用此方法的对象,我们说了静态的方法是不用对象
调用的,而是使用类名来访问,所以根本就没有对象存在,也就没有$this 这个引用了,没有
了$this 这个引用就不能访问类里面的非静态成员,又因为类里面的静态成员是可以不用对象
来访问的,所以类里面的静态方法只能访问类的静态的属性,即然$this 不存在,在静态方法
中访其它静态成员我们使用的是一个特殊的类“self”;self 和$this 相似,只不过self 是代表
这个静态方法所在的类。所以在静态方法里,可以使用这个方法所在的类的“类名”,也可以
使用“self”来访问其它静态成员,如果没有特殊情况的话,我们通常使用后者,即“self::成
员属性”的方式。
代码片段
复制代码 代码如下:


class Person{
//下面是人的静态成员属性
public static $myCountry="中国";
//这是人的静态成员方法, 通过self访问其它静态成员
public static function say(){
echo "我是".self::$myCountry."
";
}
}
//访问静态方法
Person::say();
?>

在非静态方法里可不可以访问静态成员呢,当然也是可以的了,但是也不能使用“$this”
引用也要使用类名或是“self::成员属性的形式”。
const 是一个定义常量的关键字,在PHP 中定义常量使用的是“define()”这个函数,但
是在类里面定义常量使用的是“const”这个关键字,类似于C 中的#define 如果在程序中改变
了它的值,那么会出现错误,用“const”修饰的成员属性的访问方式和“static”修饰的成员
访问的方式差不多,也是使用“类名”,在方法里面使用“self”关键字。但是不用使用“$”
符号,也不能使用对象来访问。
代码片段
复制代码 代码如下:

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; 是不允许的
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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks 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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
The difference between final, finally, and finalize in Java The difference between final, finally, and finalize in Java Feb 19, 2024 pm 12:16 PM

The difference between final, finally, and finalize in Java requires specific code examples. In Java programming, you often encounter the three keywords final, finally, and finalize. Although they are spelled similarly, they have different meanings and usages. This article will explain the differences between these three keywords in detail and give code examples to help readers better understand. 1. Final keyword The final keyword can be used for classes, methods and variables. Its function is to make the modified class

In Java, is it possible to define a constant using only the final keyword? In Java, is it possible to define a constant using only the final keyword? Sep 20, 2023 pm 04:17 PM

A constant variable is a variable whose value is fixed and only one copy exists in the program. Once you declare a constant variable and assign a value to it, you cannot change its value again throughout the program. Unlike other languages, Java does not directly support constants. However, you can still create a constant by declaring a variable static and final. Static - Once you declare a static variable, they will be loaded into memory at compile time, i.e. only one copy will be available. Final - Once you declare a final variable, its value cannot be modified. Therefore, you can create a constant in Java by declaring the instance variable as static and final. Example Demonstration classData{&am

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

What is the function of java final keyword What is the function of java final keyword Nov 25, 2022 pm 04:26 PM

In Java, final can be used to modify classes, methods and variables. The final modified class means that the class cannot be inherited by any other class, which means that this class is a leaf class in an inheritance tree, and the design of this class has been considered perfect and does not need to be modified or extended. The method in the final modified class means that the class cannot be inherited by any other class and cannot be overridden; that is, the method is locked to prevent the inherited class from changing it. final modifies a variable in a class, indicating that the variable cannot be changed once it is initialized.

C++ syntax error: const objects must be initialized when defined, how to deal with it? C++ syntax error: const objects must be initialized when defined, how to deal with it? Aug 22, 2023 am 09:13 AM

For C++ programmers, syntax errors are one of the most common problems. One of the common mistakes is that const objects must be initialized at definition time. If you encounter this situation, how should you deal with it? First, we need to understand what a const object is. The const keyword is a special type qualifier in C++ that specifies that the value of a variable cannot be changed during the execution of the program. Such variables are called "constants". If you define a const object without initializing it, you will encounter the above error. This is

18 Ways to Fix Audio Service Not Responding Issue on Windows 11 18 Ways to Fix Audio Service Not Responding Issue on Windows 11 Jun 05, 2023 pm 10:23 PM

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

What are the correct uses of the const keyword in C++ functions? What are the correct uses of the const keyword in C++ functions? Apr 11, 2024 pm 02:36 PM

Correct usage of the const keyword in C++: Using const to modify a function means that the function will not modify the parameters or class members passed in. Using const to declare a function pointer means that the pointer points to a constant function.

How to use const in c language How to use const in c language Sep 20, 2023 pm 01:34 PM

const is a keyword that can be used to declare constants, const modifiers in function parameters, const modified function return values, and const modified pointers. Detailed introduction: 1. Declare constants. The const keyword can be used to declare constants. The value of the constant cannot be modified during the running of the program. The constant can be a basic data type, such as integer, floating point number, character, etc., or a custom data type; 2. The const modifier in the function parameters. The const keyword can be used in the parameters of the function, indicating that the parameter cannot be modified inside the function, etc.

See all articles