Quick combat (5)-PHP: dynamically create attributes and methods, copy objects, compare objects, load specified files, automatically load class files, namespace-webabcd

Source: Internet
Author: User
Quick combat (5)-PHP: dynamically create attributes and methods, copy objects, compare objects, load specified files, automatically load class files, namespace-webabcd [source code download]


Quick combat (5)-PHP: dynamically create attributes and methods, copy objects, compare objects, load specified files, automatically load class files, namespaces



Author: webabcd


Introduction
PHP

  • Dynamically create attributes and methods
  • Object replication
  • Object comparison
  • Load the specified file
  • Automatically load class files
  • Namespace



Example
1. knowledge about classes 3 (dynamically create attributes and methods)
Class/class3.php

 ";} // _ Get magic method. when the obtained property does not exist or is not accessible (private), the public function _ get ($ name) is called) {echo "_ get \ $ name: {$ name}"; echo"
"; Return 999 ;}$ objClass1 = new Class1 (); // when the property you set does not exist or is not accessible (private, the corresponding _ set magic method $ objClass1-> property1 = wanglei; // inaccessible, such as private, or nonexistent // when the obtained property does not exist or is not accessible (private), the corresponding _ get magic method echo $ objClass1-> property2; echo "will be called"
"; // Used to demonstrate how to dynamically create a method (this is the so-called overload in php) class Class2 {// _ call magic method, when the called instance method does not exist or is not accessible (private), the public function _ call ($ name, $ arguments) {echo "_ call \ $ name: {$ name}, \ $ arguments :". implode (',', $ arguments); echo"
";} // _ CallStatic magic method. when the called class method does not exist or is not accessible (private), the public static function _ callStatic ($ name, $ arguments) {echo "_ callStatic \ $ name: {$ name}, \ $ arguments :". implode (',', $ arguments); echo"
";}}$ ObjClass2 = new Class2 (); // when the instance method you call does not exist or is not accessible (private, the corresponding _ call magic method echo $ objClass2-> method1 ("aaa", "bbb") is called "); // when the class method you call does not exist or is not accessible (private), the corresponding _ callStatic magic method echo Class2: method2 ("aaa ", "bbb ");


2. knowledge points of classes 4 (copying objects and comparing objects)
Class/class4.php

 ";}}$ ObjClass1 = new Class1 (); // by cloning an object, the _ clone magic method $ objClass2 = clone $ objClass1 is called; // the object copied by clone is shallow copy, that is, one-to-one value assignment between member data, all the reference attributes will still be a reference to the original variable (if you want to do deep copy, you need to write it yourself) echo $ objClass2-> field1; // output: field1echo"
"; Echo $ objClass2-> field2; // output: field2echo"
"; // If the attributes and attribute values of the two objects are equal, they are equal to" = ", if ($ objClass1 = $ objClass2) {echo '$ objClass1 = $ objClass2'; echo"
";}// If the attributes and attribute values of the two objects are equal, but not instances of the same class, they" ===" are not equal if ($ objClass1! ==$ ObjClass2) {echo '$ objClass1! = $ ObjClass2 '; echo"
";}// If the two objects are instances of the same class, they are equal to" if ($ objClass1 ===$ objClass1) {echo '$ objClass1 ===$ objClass1'; echo"
";}// If the two objects are instances of the same class, they are" === "equal $ objClass3 = & $ objClass1; if ($ objClass1 ==$ objClass3) {echo '$ objClass1 ===$ objclass3'; echo"
";}


3. knowledge about classes 5 (loading specified files and automatically loading class files)
Class/class5.php

 Name; echo"
";

Class/MyClass. class. php

 


4. knowledge points of classes 6 (namespace)
Class/class6.php

  ";}Class MyClass {public function myMethod () {echo" MyNamespace1 MyClass myMethod "; echo"
";}}// When defining a namespace, you can specify the path namespace Sub1 \ Sub2 \ MyNamespace2 {const MyConst =" MyNamespace2 MyConst "; function myFunction () {echo "MyNamespace2 myFunction"; echo"
";}Class MyClass {public function myMethod () {echo" MyNamespace2 MyClass myMethod "; echo"
";}} Namespace MyNamespace3 {// call the specified constant echo \ MyNamespace1 \ MyConst; echo in the specified namespace"
"; // Call the specified function \ MyNamespace1 \ myFunction () in the specified namespace; // instantiate the class $ obj1 = new \ MyNamespace1 \ MyClass () in the specified namespace (); $ obj1-> myMethod ();} namespace MyNamespace4 {// use the specified namespace use \ Sub1 \ Sub2 \ MyNamespace2; // You do not need to write the full namespace path, because I used echo MyNamespace2 \ MyConst; echo"
"; MyNamespace2 \ myFunction (); $ obj1 = new MyNamespace2 \ MyClass (); $ obj1-> myMethod ();} namespace MyNamespace5 {// use the specified namespace, and set its alias use \ Sub1 \ Sub2 \ MyNamespace2 as xxx; // when calling the namespace, you can use its alias echo xxx \ MyConst; echo"
"; Xxx \ myFunction (); $ obj1 = new xxx \ MyClass (); $ obj1-> myMethod ();}



OK
[Download source code]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.