PHP dynamically creates properties and methods, object replication, object comparisons, loading of specified files, automatic loading of class files, namespaces

Source: Internet
Author: User
This article mainly introduces the dynamic creation of PHP properties and methods, object replication, object comparison, loading the specified files, automatic loading class files, namespace related information, the need for friends can refer to the next

PHP Preface:

• Dynamically create properties and methods

• Copying of objects

• Comparison of objects

• Load the specified file

• Automatic loading of class files

• namespaces

Example

1. Class related Knowledge point 3 (dynamically create properties and methods)

class/class3.php

The <?php/*** class's Knowledge point 3 (dynamically created properties and methods) *///is used to demonstrate how to dynamically create properties (which is what is called Overloading in PHP) class class1{//__set Magic Method, This function is called when the Set property does not exist or is not accessible (private), public function __set ($name, $value) {echo "__set \ $name: {$name}, \ $value: {$value}"; echo "<br/>";} __get Magic method, this function is called when the obtained property does not exist or is not accessible (private), public function __get ($name) {echo "__get \ $name: {$name}"; echo "<br/& gt; "; return 999;}} $objClass 1 = new Class1 (); When you set a property that does not exist or is not accessible (private), the corresponding __set Magic method $objclass1->property1 = Wanglei is called; Inaccessible, such as private, or nonexistent//when you get a property that does not exist or is inaccessible (private), the corresponding __get magic method is called Echo $objClass 1->property2;echo "&LT;BR/ > ";//is used to demonstrate how to create a method dynamically (which is what is called Overloading in PHP) class class2{//__call Magic method, which calls this function when an instance method is not present or is not accessible (private) Call ($name, $arguments) {echo "__call \ $name: {$name}, \ $arguments:". Implode (', ', $arguments); echo "<br/>";} __callstatic Magic method, this function is called when the calling class method does not exist or is not accessible (private), public static function __callstatic ($name, $arguments) {echo "__ Callstatic \ $name: {$name}, \ $arguments: ". ImploDe (', ', $arguments); echo "<br/>";}} $objClass 2 = new Class2 (); When you invoke an instance method that does not exist or is not accessible (private), the corresponding __call magic method is called Echo $objClass 2->method1 ("AAA", "BBB") When you call a class method that does not exist or is inaccessible (private), the corresponding __callstatic magic method is called Echo class2::method2 ("AAA", "BBB");

2, class of related knowledge point 4 (Object replication, object comparison)

class/class4.php

The <?php/*** class is related to Knowledge point 4 (Object replication, object comparison) *///is used to demonstrate how to copy objects class Class1{public $field 1 = "field1";p ublic $field 2 = "field2";//through C Lone calls this magic method function __clone () {echo "__clone" when copying objects, echo "<br/>";}} $objClass 1 = new Class1 ();//Copy the object through clone, call __clone Magic method $objclass2 = Clone $objClass 1;//Copy the object through clone as a shallow copy (shallow cop Y), which is the one by one assignment between the member data, and all the reference properties will still be a reference to the original variable (if you want to do deep copy you need to write it yourself) echo $objClass 2->field1; Output:field1echo "<br/>"; Echo $objClass 2->field2; Output:field2echo "<br/>";//if the properties and property values of the two objects are equal, they "= =" are equal, if ($objClass 1 = = $objClass 2) {echo ' $objClass 1 = = $obj Class2 '; echo "<br/>";} If the properties and property values of two objects are equal, but not an instance of the same class, they "= = =" Unequal if ($objClass 1!== $objClass 2) {echo ' $objClass 1!== $objClass 2 '; Echo ' <br /> ";} If two objects are instances of the same class, they "= = =" Equal if ($objClass 1 = = = $objClass 1) {echo ' $objClass 1 = = = $objClass 1 '; echo "<br/>";} If two objects are instances of the same class, they "= = =" Equal $OBJCLASS3 = & $objClass 1;if ($objClass 1 = = = $objClass 3) {echo ' $objClass 1 = = = $ OBJCLASS3 '; Echo ' <br/> ";} 

3, the relevant knowledge point of the Class 5 (load the specified file, automatically load the class file)

class/class5.php

The <?php/*** class related knowledge point 5 (load the specified file, automatically load the class file) *//** contains and runs the specified file, either an absolute path or a relative path * include a warning if it is not found, then continue running (include_once: Include only the specified file once in the current file) * Require error if not found, then terminate run (require_once: only require specified file once in the current file) * include '; * require '; * include_ Once "; * require_once"; *///demonstrates how to implement automatic loading of classes by __autoload Magic method __autoload ($class _name) {//load specified file require_once $class _name. '. class.php ';} If the MyClass class is not found in the current file, then the __autoload Magic method $obj = new MyClass () is called, and Echo $obj->name;echo "<br/>"; Class/myclass.class.php<?phpclass myclass{public $name = "WEBABCD";}

4. Related knowledge points of Class 6 (namespace)

class/class6.php

<?php/*** Knowledge Point 6 (namespace) *///The following code is only for demonstration purposes, and it is not recommended to define multiple namespace//in a file if there is only one namespace in the current file, the following paragraph can omit the curly braces for the namespace, directly namespace MyNamespace1; You can 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{//invokes the specified constant in the specified namespace, echo \mynamespace1\myconst;echo "<br/>";//invokes the specified function in the specified namespace \ Mynamespace1\myfunction ();//Instantiate the class in the specified namespace $obj1 = new \mynamespace1\myclass (); $obj 1->mymethod ();} namespace mynamespace4{//use the namespace specified by using the \sub1\sub2\mynamespace2;//, you do not have to write the path to the full namespace, because you have previously used echo mynamespace2\ Myconst;echo "<br/>"; Mynamespace2\mYfunction (); $obj 1 = new Mynamespace2\myclass (); $obj 1->mymethod ();} Namespace mynamespace5{//uses the specified namespace and sets an alias for it using \sub1\sub2\mynamespace2 as xxx;//and then invokes the namespace, it can use its alias echo xxx\myconst; echo "<br/>"; xxx\myfunction (); $obj 1 = new Xxx\myclass (); $obj 1->mymethod ();}

The above mentioned is a small series of PHP to introduce the dynamic creation of properties and methods, object replication, object comparison, loading the specified file, automatic loading class files, namespaces related introduction, I hope to help!

  • 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.