Home  >  Article  >  Backend Development  >  Usage of public, private, protected, abstract and other keywords in PHP

Usage of public, private, protected, abstract and other keywords in PHP

小云云
小云云Original
2018-01-03 09:45:201591browse

This article mainly shares with you an article that discusses the usage of public, private, protected, abstract and other keywords in PHP. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.

Commonly used keywords in PHP

PHP contains many keywords that restrict functions and classes. Commonly used keywords are abstract, final, interface, public, protected, private, static and so on. Below we will analyze and sort out the usage of each.

The keywords of variables and methods are public, private, and protected

public has the greatest authority and can be used by subclasses and can also support calls after instantiation.

protected means protected, and the access permission can only be accessed in subclasses and this class

private means private, and can only be accessed in the current class

<?php
//
/**
* Define MyClass
*/
class MyClass
{
 public $public = &#39;Public&#39;;
 protected $protected = &#39;Protected&#39;;
 private $private = &#39;Private&#39;;
 public function printHello()
 {
 echo $this->public;
 echo $this->protected;
 echo $this->private;
 }
 protected function pro_test(){
 var_dump(1);
 }
}
$obj = new MyClass();
echo $obj->public; // 这行能被正常执行
//echo $obj->protected; // 这行会产生一个致命错误
//echo $obj->private; // 这行也会产生一个致命错误
$obj->printHello(); // 输出 Public、Protected 和 Private
$obj->pro_test();//直接报错
?>

The keyword static for variables and methods

The function of static is to enable the value or method to be called in the class without instantiation. At the same time, the variable modified by static has the same ability to store the value. Function, for example, if we do not use static, the result of running is as follows:

<?php
function test(){
 $var=1;
 echo $var."</br>";
 $var++;
}
test();// 1
test();// 1
test();// 1
?>

If we add static to the variable, it will become like this

<?php
function test(){
 static $var=1;
 echo $var."</br>";
 $var++;
}
test();// 1
test();// 2
test();// 3
?>

You may not be able to realize the benefits of PHP doing this here. , then let’s first assume that the reader is also familiar with JS. There is no static keyword in JS, so if we want to implement a program that can save the results of each program operation as the basis for the next operation, we need Write it this way.

var glo=0;
 function test(){
 glo++;
 document.writeln(glo);
 }
 test();
 test();
 test();

This will leak glo into global variables. If more variables are generated, it will lead to memory leaks (memory leaks refer to variables that occupy too much memory space and are not released)

<script>
 var glo=0;
 function test(){
 glo++;
 document.writeln(glo);
 }
 test();// 1
 test();// 2
 test();// 3 
</script>

So compared to languages ​​that do not define static, it has the advantage of retaining variables, not leaking memory, and not easily causing global variables to be misused (because the scope is not global)

$age=0;
$age++;
function test1() {
 static $age = 100;
 $age++;
 echo $age."</br>";
}
function test2() {
 static $age = 1000;
 $age++;
 echo $age."</br>";
}
test1(); // 101
test2(); // 1001

The keyword final for classes and methods

final can only be used to modify classes and functions. After using final, it cannot be inherited. For example, the following code will directly report an error

class BaseClass {
 public $public = 'Public';
 function test() {
 echo "BaseClass::test() called\n";
 }
 final public function moreTesting() {
 echo "BaseClass::moreTesting() called\n";
 }
}
class ChildClass extends BaseClass {
 public function moreTesting() {
 echo "ChildClass::moreTesting() called\n";
 }
}

Special keyword interface, abstract

The meaning of interface is to standardize the programming style. Imagine that if an interface is implemented, then when we use this interface class, we must implement the methods inside, which plays a role The role of unified naming.

Class can inherit multiple interfaces. Single inheritance between interfaces is achieved through extends. The relationship between class and interface is established through implements

Sample code:

<?php
interface testA{
 function funcA();
}
interface testB{
 function funcB();
}
interface testC extends testA {
 function funcC();
}
class run implements testC ,testB {
 public function funcA()
 {
 // TODO: Implement funcA() method.
 }
 public function funcB()
 {
 // TODO: Implement funcB() method.
 }
 public function funcC()
 {
 // TODO: Implement funcC() method.
 }
}
?>

The function of abstract is actually the same as that of interface, but all methods in interface must be implemented, but in an abstract-modified class, there can be one or more abstract-modified methods. So we can understand that interface is a special case of abstract (when all methods are abstract methods, they must be implemented). Abstract has the following characteristics:

1. As long as at least one method in the class uses the abstract keyword, then the class is abstract, and the corresponding keyword must be added

2. Abstract Methods only have the declaration part of the method and no method body.

But in my opinion, abstract has several scenarios like this in actual applications

1. Standardize the naming rules of public parts when multi-person programming (without any explanation, the principle Same as interface)

2. To prevent the parent from being instantiated directly, use the

style code as follows:

<?php
abstract class shopping
{
 public function buy()
 {
 echo "buy";
 }
 public function loan()
 {
 echo "loan";
 }
}
class leslieBuy extends shopping
{
}
//$test1=new shopping;//直接语法错误
$leslieSie = new leslieBuy;
$leslieSie->loan();//打印出loan
?>

Does everyone know their usage? Get it quickly.

Related recommendations:

usage examples and difference analysis of public, private, protected

php object-oriented public private protected Three modifier code examples

A brief discussion on the scope of access modifiers private, protected, and public in PHP

The above is the detailed content of Usage of public, private, protected, abstract and other keywords in PHP. 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