PHP common keywords such as object-oriented development-PHP Tutorial

Source: Internet
Author: User
Common keywords such as PHP object-oriented development. Common keywords of php classes include: fianl, self, static, and const) below I will sort out common keywords of php classes, including fianl, self, static, and const) next, I will sort it out.

Common keywords in the class

1. fianl: Lock
2. self: a bit similar to the this keyword
3. static: static attributes
4. const: constant keyword


1. keyword: fianl

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

1. final does not modify member attributes (this keyword is often not used in classes)

2. final can only modify classes and methods

Purpose:

Classes modified using final cannot inherit from quilt classes.

The final modification method cannot be overwritten by the quilt class.

Use fianl if the restriction class is not inherited and the method is not overwritten.


Example of using the final keyword for a class:

Final class Person
{
......
}

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

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

Example of using the final keyword:

Class Person
{
Final function say ()
{
......
}
}

Example #1 Final method Example

The code is as follows:

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 ";
}
}
// Generate Fatal error: Cannot override final method BaseClass: moreTesting ()
?>

Example #2 Final class Example

The code is as follows:

Final class BaseClass {
Public function test (){
Echo "BaseClass: test () calledn ";
}

// No matter whether you declare the method as final.
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 the member variables or methods in the PHP class, if the referenced variables or methods are declared as const (defining constants) or static (declaring static), the operator must be used:: if the referenced variable or method is not declared as const or static, the operator-> must be used.

In addition, if you access const or static variables or methods from the inside of the class, you must use self-referenced. Otherwise, if the internal access from the class is not a const or static variable or method, then you must use the self-referenced $ this


Format:

Self: Class internal member (attribute or method)
| Equivalent
Class name: Class internal member (attribute or method)

Note: Because it does not make sense to access internal attributes or methods of the mathematical and physical classes, self is generally used for static members, constants, and other definitions in the classification class.

3. keyword: static

Memory optimization is used to define static attributes or methods and can be used when the class is not instantiated. Static attributes occupy memory independently, instead of repeatedly occupying memory because multiple objects are created.

Format:
Class class1 {
Static $ name;
Static function fun1 {
...
}
}

Static Member access (inside the class ):
Self: static member
Class name: static member

Access to static members (external class ):
Class name: static member


4. keyword: const

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

Class class1 {// We recommend that you use uppercase letters instead of the $ symbol.
Const NAME;
...
}

Let's look at a small PHP const example:

The code is as follows:

<? Php
Class say_const {
Const CHARSET = "China ";
Publice function say_hello ()
{
Echo slef: CHARSET;
}
}
$ Const1 = new say_const ()
$ Const1-> say_hello ();
?>

Using (fianl), similar to the this keyword (self), static property (static), and constant keyword (const), I will sort it out for you...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.