PHP Object-related knowledge summary, php Object-related knowledge
Object transfer: the PHP Object is passed by reference. More accurately, it is passed by an alias (identifier), that is, they all store the same identifier (ID) the identifier points to the real content of the same object.
<? Php class A {public $ foo = 1;} $ a = new A; $ B = $ a; // $, $ B is a copy of the same identifier // ($ a) = ($ B) = <id> $ B-> foo = 2; echo $ a-> foo. "\ n"; // 2 $ c = new A; $ d = & $ c; // $ c, $ d is referenced // ($ c, $ d) = <id> $ d-> foo = 2; echo $ c-> foo. "\ n"; // 2 $ e = new A; function foo ($ obj) {// ($ obj) = ($ e) = <id> $ obj-> foo = 2;} foo ($ e); echo $ e-> foo. "\ n"; // 2
• Object replication: Object replication can be completed by using the clone keyword. If the original object defines the _ clone () method, the _ clone () method in the new object () the method will be called after the copy is complete. The __clone () method can be used to modify the value of the copy object attribute. After the object is copied, a shallow copy operation is performed on all the attributes of the object. However, all the referenced attributes are still references to the original variables.
<? Php class SubObject {static $ instances = 0; public $ instance; public function _ construct () {$ this-> instance = ++ self: $ instances ;} public function _ clone () {$ this-> instance = + self: $ instances;} class MyCloneable {public $ object1; public $ object2; function _ clone () {// force copy this-> object; otherwise, it still points to the same object $ this-> object1 = clone $ this-> object1 ;} function cloneTest () {echo 'clonetest'; }}$ obj = new MyCloneable (); $ obj-> object1 = new SubObject (); $ obj-> object2 = new SubObject (); $ obj2 = clone $ obj; print ("Original Object: \ n"); print_r ($ obj ); print ("Cloned Object: \ n"); print_r ($ obj2); echo $ obj2-> cloneTest (). ": \ n"; echo (new Reflectionclass ($ obj2 ));
Output result of the preceding example:
Original Object:MyCloneable Object( [object1] => SubObject Object ( [instance] => 1 ) [object2] => SubObject Object ( [instance] => 2 ))Cloned Object:MyCloneable Object( [object1] => SubObject Object ( [instance] => 3 ) [object2] => SubObject Object ( [instance] => 2 ))cloneTest:Class [ <user> class MyCloneable ] { @@ /public/t.php 18-33 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [2] { Property [ <default> public $object1 ] Property [ <default> public $object2 ] } - Methods [2] { Method [ <user> public method __clone ] { @@ /public/t.php 23 - 27 } Method [ <user> public method cloneTest ] { @@ /public/t.php 29 - 32 } }}
• Object traversal: foreach can only traverse the visible attributes of an object and cannot traverse its methods, which is easier to implement. In addition, you can also traverse object attributes by implementing the Iterator interface or IteratorAggregate interface.
• Type constraints: PHP, as a weak type language, type constraints can make programming more standardized and less prone to errors. Type constraints can not only be used in object definitions, but also in function definitions. Type constraints can specify objects, interfaces, arrays, and callable (Closure callback). Type constraints are used to ensure that the actual data types are consistent with the prototype definitions. If they are inconsistent, a fatal and captured error is thrown; however, if the default value is NULL, the real parameter can be NULL. Type constraints cannot be used for scalar types such as int or string, and Traits is not allowed.
• Object serialization and Restoration: The serialize () function can be used to compress strings containing byte streams to facilitate object storage. The unserialize () function can restore strings as objects. However, the premise is that the class definition of the object has been completed no matter the serialization or deserialization, that is, the class (File) needs to be imported first ).
• Heavy Load: PHP's heavy load includes attributes and methods. It is more like a method. It does not support common heavy load syntax specifications. It is unpredictable and has a wider scope of impact, magic methods is used to call undefined or invisible class attributes or methods in the current environment. All overload methods must be declared as public (this one should be easy to understand, and others may need you only because you cannot see it ), parameters cannot be passed through reference (the overload method is unpredictable. It is estimated that the variable is not referenced at will for security reasons ). The overloaded attribute cannot be used in other language structures except isset (), which means that when empty () is used for an overloaded attribute, the overloaded magic method will not be called; to avoid this restriction, you must assign a value to the local variable for the overload attribute and then use empty (). It can be seen that the overload attribute exists between the valid attribute and the invalid attribute.
[Attribute overload]: these methods cannot be declared as static. In static methods, these magic methods will not be called.
Public void _ set (string $ name, mixed $ value)
When assigning values to inaccessible properties, __set () will be called
Public mixed _ get (string $ name)
When you read the value of an inaccessible attribute, __get () will be called
Public bool _ isset (string $ name)
When isset () or empty () is called for an inaccessible attribute, __isset () is called.
Public void _ unset (string $ name)
When unset () is called for an inaccessible attribute, __unset () is called.
Note:
Because PHP processes the value assignment operation, the return value of __set () will be ignored. Similarly, in the chain assignment below, __get () will not be called:
$ A = $ obj-> B = 8;
[Method overload]:
Public mixed _ call (string $ name, array $ arguments)
When an inaccessible method is called in an object, __call () will be called
Public static mixed _ callStatic (string $ name, array $ arguments)
When an inaccessible method is called in a static context, __callstatic () will be called
• Static attributes and Methods: static keywords are used to define static attributes and static methods. static attributes cannot be accessed by instantiating objects> (but static methods are acceptable ). Static attributes can only be initialized as constant expressions. Therefore, static attributes can be initialized as integers or arrays, but cannot be initialized as another variable or function return value or point to an object. A variable can be used to represent a class to dynamically call static attributes. However, the value of this variable cannot be self, parent, or static.
class Foo { public static $my_static = 'foo'; public function staticValue() { return self::$my_static; } } class Bar extends Foo { public function fooStatic() { return parent::$my_static; } } print Foo::$my_static . "\n"; $foo = new Foo(); print $foo->staticValue() . "\n"; print $foo->my_static . "\n"; // Undefined "Property" my_static print $foo::$my_static . "\n"; $classname = 'Foo'; print $classname::$my_static . "\n"; // As of PHP 5.3.0 print Bar::$my_static . "\n"; $bar = new Bar(); print $bar->fooStatic() . "\n";
• Post-static binding: static: defines the working principle of post-static binding. It stores the Class Name of the previous non-forwarded call. When a static method is called, the class name is the one explicitly specified (usually on the left side of the: Operator). When a non-static method is called, that is, the class to which the object belongs. Using self: Or _ CLASS _ for static reference to the current CLASS depends on the CLASS that defines the current method; static: is no longer parsed as the CLASS that defines the current method, it is calculated during actual operation and can be used to call static attributes and all methods.
<?php class A { private $proPrivate = "private of A"; protected $proProtected = "protected of A"; public $proPublic = "public of A"; private function foo() { echo $this->proPrivate."\n"; echo $this->proProtected."\n"; echo $this->proPublic."\n"; } public function test() { $this->foo(); static::foo(); } } class B extends A { /* foo() will be copied to B, hence its scope will still be A and * the call be successful */ } class C extends A { private $proPrivate = "private of C"; protected $proProtected = "protected of C"; public $proPublic = "public of C"; private function foo() { /* original method is replaced; the scope of the new one is C */ echo "I am C\n"; } public function myFoo() { //parent::foo(); $this->foo(); } } echo "Class B:\n"; $b = new B(); $b->test(); echo "\nClass C:\n"; $c = new C(); $c->myFoo(); $c->test(); //fails
Output result of the preceding example:
Class B:private of Aprotected of Apublic of Aprivate of Aprotected of Apublic of AClass C:I am Cprivate of Aprotected of Cpublic of C Fatal error: Uncaught Error: Call to private method C::foo() from context 'A' in /public/t.php:19 Stack trace: #0 /public/t.php(54): A->test() #1 {main} thrown in /public/t.php on line 19
• Inheritance: the official document describes inheritance as follows: "When a class is extended, the subclass inherits all the public and protected methods of the parent class. Unless the subclass covers the parent class method, the inherited method retains its original function. "The implication is that the private attributes and methods will not be inherited; however, the previous example tells us that the subclass has the same attributes and methods as the parent class. Inheritance means full replication, which can meet our requirements for inherited programming. If the private class cannot be inherited, subclass must be customized and not required in most cases. The other is visibility. The private attributes and methods of the parent class are invisible in the subclass. The above example also tells us that visibility, inheritance, and static binding mechanisms should be taken into account for the domain actually executed by the object.