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

Source: Internet
Author: User
Tags autoload
PHP dynamically creates attributes and methods, copies objects, compares objects, loads specified files, automatically loads class files, namespaces, and ,. PHP dynamically creates attributes and methods, copies objects, compares objects, loads specified files, automatically loads class files, namespaces, PHP preface: dynamic Property and method object creation PHP dynamic property and method creation, object copying, object comparison, loading specified file, automatic loading class file, namespace,

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"
";} // _ 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

<? 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"; echo "is called when an object is cloned"
";}}$ 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

<? 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 () will be called (); echo $ obj-> name; echo"
"; 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"
";}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 ();}

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!

Objects, copying objects, comparing objects, loading specified files, automatically loading class files, namespaces, PHP Preface: dynamically creating attributes and repeat method objects...

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.