php中final static $this关键字学习笔记
在php中final static $this关键字主要是用在类中或对象中,下面我来把我学习final static $this笔记记录下来,有需要了解的朋友可参考参考.
final关键字
php final关键字用来在一个函数或者类名称前面,表明该函数不能被重写或者该类不能被继承.
1、final方法不能被重写.
如果不希望类中某个方法被子类重写,只需要在这个方法前加上关键字final,即设置final方法.
实例代码如下:
<?php class ex1 { final function fn1() { return "php"; } } class ex2 extends ex1 { function fn1() { return "html"; } } $p = new ex2(); echo $p->fn1(); ?>
加了final关键字的方法不能被重写,运行出错.
2、final类不能被继承.
被声明了final的类不能被继承,否则出错.
实例代码如下:
<?php final class ex1 { private $name; } class ex2 extends ex1 { private $age; } ?>
static关键字
php static关键字不仅可以用来声明一个静态变量,在类中同样可以声明静态的属性或者方法,成为“类属性”或“类方法”.
1、声明了静态的属性和方法后,在类中不能用$this关键字来引用,可以使用下面两种方法引用:
在类中:self::静态成员名称
在类外:类名称::静态成员名称
2、访问该类时,可不必实例化,直接使用类名称::静态成员名称的格式.
静态属性实例:
实例代码如下:
<?php class user { private static $count = 10; private static $sum = 1; public function __construct() { for ($i = 1; $i < 11; $i++) { self::$sum = self::$sum * self::$count; /* 调用静态变量 */ self::$count--; } } public function getCount() { return self::$sum; } } $user1 = new user(); echo $user1->getCount(); ?>
静态方法实例:
实例代码如下:
<?php class Math { public static function sum($num1, $num2) { return $num1 + $num2; } public static function product($num1, $num2) { return $num1 * $num2; } } $a = 88; $b = 100; echo "两数之和为: "; echo "<br>"; echo Math::sum($a, $b); echo "<P>"; $a = 66; $b = 88; echo "两数之积为: "; echo "<br>"; echo Math::product($a, $b); ?>
$this关键字
为了解决php类和对象中变量与属性的命名冲突和不确定性问题,引入了”$this”关键字来调用当前的对象.
在类中调用当前对象的属性和方法,必须使用”$this->”关键字;$this在构造函数中指该构造函数所创建的新对象;方法内的局部变量不属于对象,不使用$this关键字取值.
使用$this关键字,我们可以在类中调用对象属性或者方法.
1、调用变量
实例代码如下:
<?php class user { private $n; function __construct() { $name = "Mike"; echo $this->n = $name; } } $p = new user(); ?>
2、调用方法
实例代码如下:
<?php class cal { public function sum($a, $b) { return $a + $b; } public function prt($a, $c) { return $a * $c; } public function result($a, $b, $c) { $a = $this->sum($a, $b); return $this->prt($a, $c); } } $c = new cal(); echo "(2+3)*10= " . $c->result('2', '3', '10'); ?>
教程地址:
欢迎转载!但请带上文章地址^^

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In PHP development, when you want to use member variables or methods of a class, you often encounter the error message "PHPFatalerror: Using$thiswhennotinobjectcontext". This error message is caused by the wrong context when using the $this keyword to access class members. To solve this problem, below we will introduce some workarounds. Make sure the code is inside the class Check if the code is inside the class

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

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

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.

The role and usage of static in C language: 1. Variable scope; 2. Life cycle; 3. Internal function; 4. Modify global variables; 5. Modify function; 6. Other uses; Detailed introduction: 1. Variable scope, when If there is the static keyword before a variable, then the scope of the variable is limited to the file in which it is declared. In other words, the variable is a "file-level scope", which is very useful for preventing the "duplicate definition" problem of variables; 2. Life cycle, static variables are initialized once when the program starts executing, and destroyed when the program ends, etc.

1. static Please look at the following program first: publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello, world!");//(2)}} Have seen this Segment programs are familiar to most people who have studied Java. Even if you have not learned Java but have learned other high-level languages, such as C, you should be able to understand the meaning of this code. It simply outputs "Hello, world" and has no other use. However, it shows the main purpose of the static keyword.

There are two ways to create final objects in Java: declaring a final variable or declaring a class using the final modifier. When a final variable is declared, the object is created through an initializer; when a final class is declared, the class instance is immutable. Importantly, references to final objects can still change, but the objects they point to are immutable.

Practical application scenarios and usage skills of the static keyword in C language 1. Overview static is a keyword in C language, used to modify variables and functions. Its function is to change its life cycle and visibility during program running, making variables and functions static. This article will introduce the actual application scenarios and usage techniques of the static keyword, and illustrate it through specific code examples. 2. Static variables extend the life cycle of variables. Using the static keyword to modify local variables can extend their life cycle.
