Basic practice of object-oriented programming in PHP: (Understanding Class, Class-to-object instantiation, construction and destructor, object reference);
Concepts of the class:
Birds of a feather flock to a class with objects of similar characteristics.
class defines the same properties and methods that these similar objects have.
A class is a description of a similar object, a definition of a class, a blueprint or a prototype of a class object.
The object of a class is called an instance of a class,
The properties and methods of a class are collectively referred to as class members
The concept of instantiation, the definition and invocation of classes
The instantiation of a class is the creation of an object through the definition of a class
The definition of a class starts with the keyword class, followed by the name of the class. The name of a class is usually capitalized in the first letter of each word, starting with curly braces and ending
Class is instantiated as an object using the keyword New,new immediately after the name of the class and a pair of parentheses
The member properties and methods in the object can be accessed by using the symbol
constructor function
The default constructor is called automatically when an object is instantiated (writes the __construct method after all attributes, and is automatically called);
$this is a pseudo-variable inside PHP that represents the object itself. You can access the properties and methods of an object in a $this-> way. Represents the variable itself, (who is called)
Each time an object is instantiated with new, the constructor is called with the argument list following the class name
Data_default_timezone_set ("PRC"); Set default time zone in PHP header; Set default time zone for all datetime functions
Destructors
Destruct destructor keyword (automatically called when the program executes to the end);;
function destruct () {} destructors, unlike constructors, constructors must be called when new instantiates an object
Destructors are instantiated objects that call the destructor after all programs have finished running, and are based on the principle of instantiating the object after the first out, unless the destructor is triggered during execution, the destructor is called immediately
Set the instantiation variable to null NULL to invoke the destructor immediately (triggering the destructor immediately) (provided that the instantiated object is not referenced and is no longer in use, and the destructor is immediately executed if NULL is set);)
After instantiating an object and accessing the member properties, then instantiating the second object, and then continuing to access the member properties, the destructor is not immediately triggered until the execution of the program ends (that is, to the end)
Destructors are called automatically when a class is instantiated and is no longer referenced
Destructors are often used to clean up resources used by the program. For example, if the program uses a printer, you can release the printer resources inside the destructor.
Self-total: Each instantiation of a class executes a construct and destructor once, and the PHP program executes the destructor of the class after the instantiation of the object, followed by the first-out principle, unless the destructor is invoked immediately during execution.
The construction and destructor must be instantiated before the class is executed;
References to Objects
The __destruct () function is called when all references to the object are all set to NULL
$james =new nbaplayer (' params ');
$james 1 = $james; It is equivalent to copying more than one reference, and the two are independent two references.
$james 2 = & $james; The equivalent of an alias for James, which is actually one, with just two names.
PHP High-level practice for object-oriented programming: (Inheritance, access control, statically static keyword, rewrite, Final, interface, polymorphic, abstract class)
Object-oriented-inheritance
Extract the attributes and methods you want to summarize into a large class class, and then let the subclass object inherit the properties and methods that you want, and you can access the methods and properties defined in the parent class directly on the object of the child class.
In PHP, you can use the extends keyword to represent the class's inheritance, followed by the class name of the parent class (also called the base class).
PHP extends can only follow the class name of a class, this is the single-inheritance principle of PHP.
The properties and methods defined in the parent class can be accessed using $this.
Rewrite (Overwrite):
Once the subclass modifies the properties and methods of the parent class, it executes as soon as the child class modifies the function.
Access control:
Object-oriented three access rights
Public itself its subclasses other classes
Protected its own and its subclasses
Private its own
Private cannot be accessed by the quilt class, in order to access the private element:
You can define the Getprivate function and the Setprivate function to control
Static member Static
Add the Static keyword after the access control keyword when you define it
(Access control Keyword: public. protected. Private)
1. Static properties are used to hold the class's public data
2. Static methods can only access static properties
3. Static members can be accessed without instantiating the object, are global, and the modification is both global. If static is not defined, the instantiated modifications are independent.
Static members do not need to create objects to be accessible, $this to be used in objects, so ...
Static methods and property definitions use the static keyword; access to a static property must be added $ after::
The static method cannot use $this to get the static property but to use self:: or static:: or parent::;
That
(in the current Class) static method inside (only access static member property) using static member with self/static::+ member name
Static methods use parent::+ member names when using static members in the parent class
External access to a static method or static member property with the class name +::+ the method name/member property name (not static properties and methods cannot be accessed in this way;).
Final member; (not supported before PHP5)
Overrides are not necessarily identical to the parent class, and can be overridden as long as the method name is consistent
A method with a keyword final cannot be overridden, and a class with the final keyword cannot be inherited
Supplement to data access:
The parent keyword can be used to invoke methods that are overridden by the quilt class in the parent class; The Self keyword can be used to access member methods of the class itself or to access the constants defined in its own static members and classes, but it cannot be used to access the properties of the class itself; The constant is not required to precede the constant name with the $ sign; (const abc= "method for defining constants in a class") The static keyword is used to access statically defined members, and a $ symbol is required to access static members; Property method remarks
$this, $ dynamic, no
Self: const, silent, dynamic accessible parent class const, Access static property to add $ before the sign
Parent:: const, silent, dynamic override method that invokes the same name as
Static:: const, silently accessing the property to add $ before the $ symbol
Static, dynamic properties and methods can be overridden;
Copy Code
Interface interface
The interface inherits the keyword implement;
The method of the class must be implemented, and the method of the interface must be empty.
A class can inherit multiple interfaces: Class A implements b,c{}
The keyword for interface inheritance interfaces is also extend; Subclasses must implement methods for all interfaces; interfaces cannot create their own objects.
Classes can be connected to multiple interfaces, interfaces can inherit interfaces, all methods of the interface default public, the implementation of the implementation in the class, the interface method is empty. There are several methods in the interface, the sub-class must implement several methods, which is the role of the interface, can be unified name;
Instanceof is used to determine whether an object implements an interface, which can be avoided by risk.
Polymorphic
For a method inside the same interface, it can be implemented by multiple classes, which is polymorphic. The main interface is to achieve polymorphism.
Abstract class
The methods inside the interface are not implemented, and the methods inside the class are all implemented, so the abstract class is allowed to be implemented within the class.
Inheriting the common methods and abstract class methods of abstract classes; Keywords with extends; only one abstract class can be inherited;
PHP is simply Java and C + + object-oriented mixed into a piece, I was messy, a few of the real gas disorderly channeling, not clear
Abstract is the combination of interface and extends.
Magic method
such as creating an instance of a Magictest class
$obj =new magictest ();
Construct (), destruct ();
ToString (); Echo $obj; is automatically called when the object is converted to a string.
Invoke (); $obj (4); is called automatically when the object is called as a method.
Call (); This method is called automatically when an object accesses a method name that does not exist.
① Invocation Example: Public function call ($name, $argument) {}
② Note: The access control keyword must be public; There must be two parameters: the method name ($name) that the object accesses, the parameter that the method contains ($argument, array).
Callstatic (); This method is called automatically when an object accesses a method name that does not exist. Note Ibid.
Call () and) callstatic () are also known as overloads of the method (overloading) in PHP. The invocation of the name of the same method can be implemented by these two methods to correspond to different methods.
Overload of the Property Get (), set (), Isset (), unset (), Empty ()
Get (), set (); $obj->name = "Name value", which is called separately when reading and assigning a value to an inaccessible property;
Isset (); Isset ($obj->name) triggers the Isset () Magic method when calling Isset () and empty () on an inaccessible property
Note that isset and empty are not the opposite relationship, Isset determines whether the value is defined, and empty considers that the return true is undefined.
Unset (); Call Unset when unset is called on a non-accessible property;
Cannot echo ' $obj->name is empty? '. unset ($obj->name);
Clone Can form a new object and invoke the Clone () method inside the class;
Clone duplicates objects that do not interfere with the original object, and can be re-assigned with __clone () to the copied results.
Implode (, $arr) separates the array with "," to open the output
Object oriented
Internal is high cohesion, external is low coupling. Object-oriented design pursuit: Cohesion poly-low coupling
The Magic method is divided into three groups:
1,tostring (); Invoke ();
2,call (); Callstatic ();
3,get (); set (); Isset (); unset (); __clone ();
PHP Object-oriented concept is so much, in fact, and the front-end object-oriented, much closer to the idea of ES6 object-oriented, not too difficult. But to use the actual application in the project, can play the greatest role. (Source: Programmer)
PHP Object-oriented junior high level advanced