Home  >  Article  >  Backend Development  >  PHP class modifiers and access modifiers

PHP class modifiers and access modifiers

不言
不言Original
2018-07-06 16:25:482467browse

This article mainly introduces the class modifiers and access modifiers of PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it

Class modifiers

  • 1. The class modified by abstract is an abstract class. If a class contains an abstract method, then the class is an abstract class (of course there is no abstract method in a class, we can also define this class as an abstract class ), so what is an abstract method? An abstract method is a method without a method body (without curly braces and content inside). Abstract methods are also modified in front of them. The abstract class itself cannot be instantiated, only a class inherits it and overrides all its abstract methods, so that we can instantiate its subclasses

abstract class My{
    abstract function say();
}
//
abstract class My{
     function say(){
        echo 'hello';
    }
}

  • 2. The class modified by interface is an interface. Interfaces are somewhat similar to abstract classes, but their difference is that all methods of the interface are abstract methods. There is no abstract modification in front of the abstract methods of the interface, and the member attributes must be constants. . Interface references are different from class inheritance keyword extends. Inheritance can only be single, while interfaces can use the keyword implements to have multiple references separated by commas.

interface demo { 
const NAME = "常量对象属性"; 
function fun1(); 
function fun2(); //抽象方法。 
} 
class MyPc extends Root implements demo , demo2 , demo3 { 
... 
}
  • 3. The class modified by final is a terminal class and cannot be inherited.

Access modifiers

  • 1.public public methods and properties can be used in all classes


  • 2.static static methods and variables

If access control permissions allow, you can use the class directly without creating an object of this class When called by adding two colons "::" to the name, there is only one copy of the static attribute in the memory, which is shared by all instances. Static methods cannot call non-static attributes. Non-static properties cannot be called using self::.

function test(){
static $a=1;
$a*=2;
echo $a."\n";
}
test()
test()
test()
//打印结果
//2
//4
//8

The initialization assignment of static variables is only once, and subsequent test methods will not initialize it again.

  • 3. Private modified methods and variables are private and can only be called in this class (when this class is instantiated in other classes, private modified methods and variables cannot be called) Variables)


  • ##4.protected methods and variables can only be used in this class and subclasses of this class


  • #5.Final modified methods cannot be overridden.

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to quickly generate modern forms with PHP

php implements shared memory process communication function (_shm )

The above is the detailed content of PHP class modifiers and access modifiers. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:PHP batch updateNext article:PHP batch update