Attributes
The variable members of a class are called "properties", or "fields" and "features", which are collectively referred to as "properties" in this document. A property declaration begins with the keywords public , protected , or private , followed by an ordinary variable declaration. Variables in properties can be initialized, but the initialized value must be a constant. The constant here means that the PHP script can get its value at the compile stage, and does not rely on runtime information to evaluate.
In the member method of the class, you can use -> (object operator): $this->property (where property is the name of the property) to access non-static properties. Static properties are accessed with :: (double colon): self::$property
class constant
You can define constants for values that remain constant in a class. There is no need to use the $ sign when defining and using constants .
The value of the constant must be a fixed value, not a variable, a class attribute, the result of a mathematical operation or a function call, it can be an array
class Test { const COLOR = array('yellow','white','black'); function showColor(){ var_dump(self::COLOR); } }
- Constants, like properties, can be inherited
- Interfaces can also define constants
autoload
In PHP 5, the spl_autoload_register() function can register any number of autoloaders to be automatically loaded when using classes and interfaces that have not yet been defined. By registering the autoloader, the scripting engine has one last chance to load the required classes before PHP fails with an error.
The spl_autoload_register() parameter is a callback function that can be registered multiple times, e.g.
<? php // MyClass is located under ./MyClass/MyClass.php, and MyClass inherits MyClass1 in ./MyClass1/MyClass1.php function load_MyClass($class_name) { // Be sure to judge whether the file exists before quoting, to avoid an error if the file does not exist if ( file_exists ('MyClass/' . $class_name . '.php' )){ require_once 'MyClass/' . $class_name . '.php' ; } } function load_MyClass1($class_name) { if (file_exists('MyClass1/' . $class_name . '.php')){ require_once 'MyClass1/' . $class_name . '.php'; } } spl_autoload_register('load_MyClass'); spl_autoload_register('load_MyClass1'); $c = new MyClass(); $c->say();
Constructor
public function __construct ([ mixed $args [, $... ]] )
- If a constructor is defined in a subclass, its superclass's constructor will not be called implicitly. To execute the constructor of the parent class, you need to call parent::__construct() in the constructor of the child class.
- If the subclass does not define a constructor, it will inherit from the superclass like a normal class method (if it is not defined as private).
- Subclass constructor parameters can be different from those in the parent class (other methods cannot)
- The constructor cannot use return to return the result, because this will end the request
destructor
public function __destruct ( void )
The destructor is called even when the script is terminated using exit() . Calling exit() in the destructor will abort the rest of the shutdown operation.
inherit
When extending a class, the subclass inherits all the public and protected methods of the superclass. Unless the subclass overrides the superclass method, the inherited method retains its original functionality.
- Unless autoloading is used, a class must be defined before it can be used. If one class extends another, the parent class must be declared before the child class. This rule applies to classes inheriting other classes and interfaces.
- PHP does not support multiple inheritance, but it does not mean that subclasses cannot inherit multiple parent classes. We can achieve this by inheriting a parent class, and the parent class inherits the method of another parent class.
class A { // more code here } class B extends A { // more code here } class C extends B { // more code here }
rewrite
When a subclass overrides a method in its superclass, PHP will not call the overridden method in the superclass. It is up to the child class whether to call the method of the parent class or not. Subclasses can be manually invoked via parent
class MyClass { protected function myFunc() { echo "MyClass::myFunc()\n"; } } class OtherClass extends MyClass { // Override the definition of the parent class public function myFunc() { // But you can still call the overridden method in the parent class parent:: myFunc(); echo "OtherClass::myFunc()\n" ; } }
Range Resolution Operator
The scope resolution operator (also known as Paamayim Nekudotayim), or more simply a pair of colons, can be used to access static members, class constants , and can also be used to override properties and methods in a class
When referencing these items outside the class definition, use the class name
Static property Static keyword
Just like all other PHP static variables, static properties can only be initialized as literals or constants, not expressions. So a static property can be initialized to an integer or an array, but not to another variable or function return value, nor can it point to an object
Calling static properties: self::static::
the difference
- self points to the class to which it was defined
- static points to the class at the time of the call, the same as $this, but $this cannot call static properties and methods
class a{ static protected $test="class a"; public function static_test(){ echo static::$test; // Results class b echo self::$test; // Results class a } } class b extends a{ static protected $test="class b"; } $obj = new b(); $obj->static_test();
anonymous class
PHP7 supports instantiating an anonymous class via new class, which can be used as a replacement for some "burn-in" full class definitions.
<?php interface Logger { public function log(string $msg); } class Application { private $logger; public function getLogger(): Logger { return $this->logger; } public function setLogger(Logger $logger) { $this->logger = $logger; } } $app = new Application; // Create anonymous class with new class $app ->setLogger( new class implements Logger { public function log ( string $msg ) { print ( $msg ); } }); $app ->getLogger()-> log ("My first log" ); ?>