Home >Backend Development >PHP Tutorial >Common keywords such as PHP object-oriented development_PHP tutorial

Common keywords such as PHP object-oriented development_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:50:14957browse

Commonly used keywords for classes in PHP include: lock (fianl), somewhat similar to this keyword (self), static attribute (static), and constant keyword (const). Let me sort it out for you.

Common keywords in the class

1.fianl: lock
2.self: somewhat similar to this keyword
3.static: static attribute
4.const: constant keyword


1. Keyword: fianl

An important keyword used to define classes and methods. When defining a class, the class cannot be inherited. When used to define a method, the method cannot be overloaded.

1. Final cannot modify member attributes (this keyword is often not used in classes)

2. final can only modify classes and methods

Function:

Classes modified with final cannot be inherited by subclasses

Methods modified with final cannot be overridden by subclasses

Used to restrict classes from being inherited and methods from being overridden, use fianl


Examples of classes using the final keyword:

final class Person
{
 …
}

When a class defined by final is inherited, the following error will be prompted:

Fatal error: Class Student may not inherit from final class (Person) in ...

Example of method using final keyword:

class Person
{
final function say()
{
       …       …
}
}

Example #1 Final method example

The code is as follows Copy code
 代码如下 复制代码

class BaseClass {
public function test() {
echo "BaseClass::test() calledn";
}

final public function moreTesting() {
echo "BaseClass::moreTesting() calledn";
}
}

class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() calledn";
}
}
// 产生 Fatal error: Cannot override final method BaseClass::moreTesting()
?>

class BaseClass {

Public function test() {
代码如下 复制代码

final class BaseClass {
public function test() {
echo "BaseClass::test() calledn";
}

// 这里无论你是否将方法声明为final,都没有关系
final public function moreTesting() {
echo "BaseClass::moreTesting() calledn";
}
}

class ChildClass extends BaseClass {
}
// 产生 Fatal error: Class ChildClass may not inherit from final class (BaseClass)
?>

echo "BaseClass::test() calledn"; } final public function moreTesting() { echo "BaseClass::moreTesting() calledn"; } } class ChildClass extends BaseClass { Public function moreTesting() { echo "ChildClass::moreTesting() calledn"; } } // Generate Fatal error: Cannot override final method BaseClass::moreTesting() ?> Example #2 Final class example
The code is as follows Copy code
final class BaseClass {<🎜> Public function test() {<🎜> echo "BaseClass::test() calledn";<🎜> }<🎜> <🎜> // It doesn’t matter whether you declare the method as final or not <🎜> final public function moreTesting() {<🎜> echo "BaseClass::moreTesting() calledn";<🎜> }<🎜> }<🎜> <🎜>class ChildClass extends BaseClass {<🎜> }<🎜> // Generate Fatal error: Class ChildClass may not inherit from final class (BaseClass)<🎜> ?>

2. Keyword: self

When accessing member variables or methods in a PHP class, if the referenced variable or method is declared as const (defining constant) or static (declaring static), then you must use the operator::, otherwise if it is referenced The variable or method is not declared as const or static, then the operator -> must be used.

In addition, if you access a const or static variable or method from within the class, you must use self-reference. On the contrary, if you access a non-const or static variable or method from within the class, you must use self-reference. $this


Format:

self::Class internal members (properties or methods)
|| Equivalent to
Class name::Class internal members (properties or methods)

Note: Because it is meaningless to access internal properties or methods without a mathematical class, self is generally used to access: static members, constants, and other defined content in the class.

3. Keyword: static

Memory optimization, used to define static properties or methods, which can be used when the class has not been instantiated. Static properties occupy memory alone and do not repeatedly occupy memory by creating multiple objects.

Format:
class class1 {
static $name;
static function fun1 {
...
}
}

Access to static members (inside the class):
self::static member
Class name::static member

Access to static members (outside the class):
Class name::static member


4. Keyword: const

is used to define constants in a class and can only modify member attributes in the class.
Format:

class class1 { //It is recommended to use uppercase definitions and do not use the $ symbol.
const NAME;
...
}

Look at a small example of PHP const:

The code is as follows
 代码如下 复制代码

< ?php
class say_const{
const CHARSET="中国";
publice function say_hello()
{
echo slef::CHARSET;
}
}
$const1=new say_const()
$const1->say_hello();  
?>  

Copy code
< ?php

class say_const{

const CHARSET="China";

publice function say_hello() echo slef::CHARSET; } } $const1=new say_const() $const1->say_hello(); ?>
http://www.bkjia.com/PHPjc/632643.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632643.htmlTechArticleCommon keywords for classes in php include: lock (fianl), somewhat similar to this keyword (self) , static attributes (static), constant keywords (const), let me sort them out for you...
Statement:
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