Access modifier
Modifier | describe |
---|---|
Public | Can be accessed internally and externally within a class |
Private | Access only within a class |
Protected | Access within and in subclasses of a class (over the entire inheritance chain) |
Method modifier
Static (static)
class Person { public static $national = 'China'; public static function show() { echo "This is a static method"; } } //test echo Person::$national,'<br>'; Person::show();
1. Static modifies attributes into static attributes, and static modifies methods into static methods.
2. Static attributes allocate space when loading classes, so they can be accessed directly without instantiation.
3. Access static member grammar: class name: static member
4. Static members have only one copy in memory, which is common to all instances.
5. Static members can not access ordinary members, only static members. Because static members may not have been instantiated yet when they were created
6. Static variables are generated when classes are loaded, and the page is destroyed after execution.
self
self represents the name of the current class
class User { public static $count = 0; public function __construct(){ self::$count++;//Represents the name of the current class } public function (){ self::$count--; } } $user1 = new User(); $user2 = new User();
Static members can be inherited
header("Content-type: text/html; charset=utf-8"); class Person { public static $nation = 'China'; public static show(){ echo "This is a static method of the parent class"; } } class Student extends Person{ } echo Student::$national,'<br>'; Student::show();
Determine the current object (static delay binding)
[static:: static member] is called delay binding. static represents a class, specifying which class is determined at runtime and which class the current object is in.
class Person { public static $type='Human beings'; public function showPerson() { //var_dump($this); //object(Student)#1 (0) { } //echo self::$type; //human echo static::$type; //Student [Static Delay Binding] } } class Student extends Person { public static $type='Student'; public function showStu() { //var_dump($this); //object(Student)#1 (0) { } //echo self::$type; //student echo static::$type; //Student [Static Delay Binding] } } //test $stu=new Student; $stu->showPerson(); echo '<hr>'; $stu->showStu();
Final (final)
final-modified classes cannot be inherited
The final modifier cannot be rewritten
abstruct (abstract)
1. The method of abstruct modification is called abstract method, and the class of abstruct modification is called abstract class.
2. If only the declaration of methods and no implementation of methods become abstract methods
3. If one method in a class is an abstract method, then the class is an abstract class.
4. Abstract classes are not allowed to be instantiated. They must be re-implemented in subclasses to be instantiated.
//abstract class abstract class Goods { protected $name; public function setName($name) { $this->name=$name; } abstract public function getName(); //Abstract method } //Implementing abstract classes class Book extends Goods { public function getName() { echo "<{$this->name}>"; } } //test $book=new Book(); $book->setName('PHP Beginning to Proficiency'); $book->getName();
The role of abstract classes:
1. Define a uniform method name
2. Preventing instantiation
interface
1. If all methods in a class are abstract methods, the class can be declared as an interface.
2. Interface is a special abstract class
3. The abstract method in the interface can only be public, and the default is public permission.
4. Implementing interfaces through implements classes
5. The abstract method in the interface cannot be modified by final and abstract
interface Goods { function add(); function update(); } class GoodsA implements Goods { public function add() { echo 'Realization add<br>'; } public function update() { echo 'Realization update<br>'; } } //test $goods=new GoodsA(); $goods->add(); $goods->update();
Interface allows multiple implementations
Classes do not allow multiple inheritance and interfaces allow multiple implementations
Inheritance class implements interface at the same time