PHP Cloning automatic loading

Source: Internet
Author: User
Tags autoload spl

Automatically load class library files

When the class is more, for example, to load 3 class library files in a file: a.class.php,b.class.php,c.class.php to use three

Require_once (' classes/a.class.php);
Require_once (' classes/b.class.php);
Require_once (' classes/c.class.php);
Can be handled with the PHP5 auto-load feature: In the global application configuration file, define a special function __autoload ($class) function (__autoload is not a method of a class, just a separate function, and the class does not have a relationship ):
function __autoload ($class)

{
Require_once ("classes/$class)
}
It doesn't matter where the function is, nor does it have to call this autoload function when creating an instance of the class. PHP is automatically completed. However, it is important to note that "the class name used to create the instance on the invocation page", and "called File name", and "the name of the class in the file" 3 must be the same. This does not need to call __autoload (); If not, you must call __autoload (' C ') separately, and give it a filename prefix.

About the root path: PHP inside "/" represents the root path: the disk where the file is located such as: d:/
HTML inside/representing root path: refers to server folder: www

    function __autoload ($class _name) {          echo ' __autload class: ', $class _name, ' <br/> ';      }      function ClassLoader ($class _name) {          echo ' SPL load class: ', $class _name, ' <br/> ';      }      Spl_autoload_register (' ClassLoader ');      New Test ();/results: SPL load Class:test  

grammar spl_autoload_register  ([callback $autoload _function])     accepts two parameters: one is the function added to the auto-load stack, and the other is the flag that the loader cannot find when the class throws an exception. The first parameter is optional and, by default, points to the spl_autoload () function, which automatically finds the path with a lowercase class name and a. php extension or an. ini extension, or any registered to Spl_autoload_extensions () The file for other extensions in the function.

Include ("/wamp/www/0607/ren.class.php"); include "Ren.class.php"; Require ("Ren.class.php"); require "Ren.class.php" ; Require_once ("Ren.class.php"); require_once "Ren.class.php";

    if (false = = = Spl_autoload_functions ()) {              if (function_exists (' __autoload ')) {                  spl_autoload_registe (' __ AutoLoad ', false);              }           }     

The Spl_autoload_functions () function returns an array of registered functions and returns a Boolean value of False if the SPL auto-load stack has not been initialized. Then, check to see if there is a function named __autoload () that, if present, can register it as the first function in the auto-load stack, preserving its functionality. After that, you can continue to register the auto-load function.

You can also call the Spl_autoload_register () function to register a callback function instead of providing a string name for the function. If you provide an array such as array (' class ', ' method '), it makes it possible to use an object's method.

PHP Replication Object Object Replication

In most cases, we do not need to completely copy an object to get its properties. But there is one case where it really needs to be: If you have a GTK window object, the object holds the window-related resources. You may want to copy a new window, keeping all properties the same as the original window, but it must be a new object (because if it is not a new object, the change in one window will affect the other window). Another case: If object A holds a reference to object B, when you copy object A, the object you want to use is no longer an object B but a copy of B, then you must get a copy of object A.

Object replication can be done by using the Clone keyword (if the __clone () method exists in the object, it is called first). The __clone () method in the object cannot be called directly.

$copy _of_object = Clone $object;

Class ren{public $name;  Public $sex;    function __construct ($n, $s) {$this->name= $n;  $this->sex = $s; } function __clone () {$this->name = "John Doe";//this represents the Replica object//$that->name = "Lisi";//that represents the original, later abandoned}}//clone $R1 = Clone $r, which is used to copy objects
PHP Object-oriented __tostring ()

__tostring () is a quick way to get the string information of an object quickly, and it seems that the magic method has an "automatic" feature, such as automatic acquisition, automatic printing, and so on, __tostring () is no exception, it is the method that is called automatically when the object reference is directly exported.

The role of __tostring ()

When we debug the program, we need to know if we can get the correct data. For example, when printing an object, look at the properties of this object, what the value is, if the class defines the ToString method, in the test, echo prints the object body, the object will automatically call its own class definition of the ToString method, formatted output this object contains data.



PHP Cloning automatic loading

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.