Detailed discussion on the usage of public private protected abstract and other keywords in PHP

  • 2021-08-31 07:24:55
  • OfStack

Keywords commonly used in PHP

There are many keywords that restrict functions and classes in PHP, such as abstract, final, interface, public, protected, private, static, etc. We will analyze and sort out the usage of each keyword below.

Keywords public, private, protected for variables and methods

public has the most permissions, which can be used by subclasses or can be called after instantiation.

protected is protected, and the access permission is that it can only be accessed in subclasses and this class

private is private and can only be accessed in the current class


<?php
//
/**
* Define MyClass
*/
class MyClass
{
 public $public = 'Public';
 protected $protected = 'Protected';
 private $private = 'Private';
 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; //  This line can be executed normally 
//echo $obj->protected; //  This line will produce 1 A fatal mistake 
//echo $obj->private; //  This line will also produce 1 A fatal mistake 
$obj->printHello(); //  Output  Public , Protected  And  Private
$obj->pro_test();// Direct error reporting 
?>

Keyword static for variables and methods

The function of static is to be able to invoke values or methods without instantiation in classes. At the same time, variables modified by static have the function of storing values. For example, we do not use static to run the results 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
?>

Here, we may not be able to realize the benefits of PHP, so let's first assume that readers are familiar with JS, and there is no keyword static in JS, so if we want to realize a program that can save the results of every program operation as the basis for the next operation, we need to write it like this.


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 leakage (memory leakage means that variables occupy too much memory space and have no other release)


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

So it has one advantage over languages that don't define static: It keeps variables, doesn't leak memory, and doesn't easily cause global variables to be misused (because the scope isn't 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

class and method keyword final

final can only be used to modify class and function. After using final, it cannot be inherited. For example, the following code will directly report errors


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 keywords interface, abstract

The significance of interface lies in the standard programming style. Imagine that if an interface is implemented, we must implement the methods inside when using this interface class, which plays the role of naming the system 1.

class can inherit multiple interfaces, single inheritance between interfaces is realized by extends, and the relationship between class and interfaces is established by 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 the class modified by abstract, there can be one or more abstract modification 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 there is at least one method in the class that uses the abstract keyword, then the class is abstract, and the corresponding keyword should be added

2. Abstract method, only the declaration part of the method, no method body.

But in my opinion, abstract has several scenarios in actual application

1. Standardize the naming rules of common parts when multiplayer programming (without any explanation, the principle is similar to interface1)

2. The implementation does not allow the parent to instantiate the

The style code is as follows:


<?php
abstract class shopping
{
 public function buy()
 {
 echo "buy";
 }
 public function loan()
 {
 echo "loan";
 }
}
class leslieBuy extends shopping
{
}
//$test1=new shopping;// Direct syntax error 
$leslieSie = new leslieBuy;
$leslieSie->loan();// Print out loan
?>

Related articles: