PHP dynamically creates attributes and methods, copies objects, compares objects, loads specified files, automatically loads class files, namespaces,

Source: Internet
Author: User
Tags autoload

PHP dynamically creates attributes and methods, copies objects, compares objects, loads specified files, automatically loads class files, namespaces,

PHP preface:

• 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

<? Php/*** class related knowledge point 3 (dynamically create attributes and methods) * // used to demonstrate how to dynamically create attributes (this is the so-called overload in php) class Class1 {// _ set magic method. When the set attribute does not exist or is not accessible (private), this function is called public function _ set ($ name, $ value) {echo "_ set \ $ name: {$ name}, \ $ value: {$ value}"; echo "<br/> ";} // _ 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 "<br/>"; 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 will be called; // inaccessible, such as private, or non-existent // when the obtained property does not exist or is not accessible (private, the corresponding _ get magic method echo $ objClass1-> property2; echo "<br/> "; // 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 "<br/>";} // _ 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 "<br/>" ;}}$ 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"); // when the class method you call does not exist or is not accessible (private), the corresponding _ callStatic magic method echo Class2 will be called :: method2 ("aaa", "bbb ");

2. knowledge points of Classes 4 (copying objects and comparing objects)

Class/class4.php

<? Php/*** class related knowledge point 4 (Object replication, object comparison) * // used to demonstrate how to copy an object class Class1 {public $ field1 = "field1 "; public $ field2 = "field2"; // This magic function _ clone () {echo "_ clone" is called when an object is cloned "; echo "<br/>" ;}}$ objClass1 = new Class1 (); // by cloning an object, the _ clone magic method $ objClass2 = clone $ objClass1; // 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; // o Utput: field1echo "<br/>"; echo $ objClass2-> field2; // output: field2echo "<br/> "; // if the attributes and attribute values of two objects are equal, they are equal to "=". if ($ objClass1 = $ objClass2) {echo '$ objClass1 = $ objClass2 '; echo "<br/>" ;}// if the attributes and attribute values of the two objects are the same, but they are not instances of the same class, then they "===" are not equal if ($ objClass1! ==$ ObjClass2) {echo '$ objClass1! ==$ ObjClass2 '; echo "<br/>" ;}// if the two objects are instances of the same class, then they are equal to if ($ objClass1 ===$ objClass1) {echo '$ objClass1 ===$ objClass1'; echo "<br/> ";} // if the two objects are instances of the same class, they are equal to $ objClass3 = & $ objClass1; if ($ objClass1 = $ objClass3) {echo '$ objClass1 ===$ objclass3'; echo "<br/> ";}

3. Knowledge about classes 5 (loading specified files and automatically loading class files)

Class/class5.php

<? Php/*** class knowledge point 5 (loading a specified file and automatically loading a class file) * // ** contains and runs the specified file, if the absolute path or relative path * include cannot be found, the system will warn you and continue running (include_once: only include the specified file once in the current file) * If require cannot be found, an error is returned and the running is terminated (require_once: only the file is specified once in the current file) * include ''; * require''; * include_once ''; * require_once ''; * // demonstrate how to use the _ autoload magic method to automatically load function _ autoload ($ class_name) {// load the specified file require_once $ class_name. '. class. php ';} // If the MyClass class cannot be found in the current file The _ autoload magic method $ obj = new MyClass (); echo $ obj-> name; echo "<br/>"; class/MyClass. class. php <? Phpclass MyClass {public $ name = "webabcd ";}

4. knowledge points of Classes 6 (namespace)

Class/class6.php

<? Php/*** class related knowledge point 6 (namespace) * // the following code is only used for demonstration, in actual projects, it is not recommended to define multiple namespaces in one file. // if the current file only has one namespace, the following section can omit the braces Of The namespace and directly use namespace MyNamespace1; namespace MyNamespace1 {const MyConst = "MyNamespace1 MyConst"; function myFunction () {echo "MyNamespace1 myFunction"; echo "<br/>";} class MyClass {public function myMethod () {echo "MyNamespace1 MyClass myMethod"; echo "<br/>" ;}}// when defining a namespace, you can specify the path namespace Sub1 \ Sub2 \ MyNamespace2 {const MyConst = "MyNamespace2 MyConst"; function myFunction () {echo "MyNamespace2 myFunction"; echo "<br/> ";} class MyClass {public function myMethod () {echo "MyNamespace2 MyClass myMethod"; echo "<br/> ";}}} namespace MyNamespace3 {// call the specified constant echo \ MyNamespace1 \ MyConst; echo "<br/> "; // 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 echo MyNamespace2 \ MyConst; echo "<br/>"; MyNamespace2 \ myFunction (); $ obj1 = new MyNamespace2 \ MyClass (); $ obj1-> myMethod ();} namespace MyNamespace5 {// use the specified namespace and set its alias to use \ Sub1 \ Sub2 \ MyNamespace2 as xxx; // when calling the namespace, you can use its alias echo xxx \ MyConst; echo "<br/>"; xxx \ myFunction (); $ obj1 = new xxx \ MyClass (); $ obj1-> myMethod ();}

The above section describes how to use PHP to dynamically create attributes and methods, copy objects, compare objects, load specified files, automatically load class files, and namespaces, hope to help you!

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.