PHP magic function _ usage of autoload and some problems _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags php cli
PHP magic function _ autoload usage and some problems. This article describes a new function of php5. we will introduce the usage and some problems of PHP magic function _ autoload, the following summarizes some problems and notes in usage. This article describes a new function of php5, let's introduce the usage and some problems of PHP magic function _ autoload. The following summarizes some problems and precautions in the usage process.

_ Autoload () usage

Php Manual

Automatically load objects
Many developers create a PHP source file for each class definition when writing object-oriented applications. A major headache is that you have to write a long list of contained files at the beginning of each script (each class has one file.

In PHP 5, this is no longer needed. You can define a _ autoload function, which is automatically called when trying to use a class that has not been defined. By calling this function, the script engine has the last chance to load the required class before a PHP error fails.

Note:

The exception thrown in the _ autoload function cannot be caught by the catch statement block and cause a fatal error.


Note:

If the php cli interaction mode is used, Autoloading does not exist.

Example #1 Autoload Example

In this example, we try to load the MyClass1 and MyClass2 classes from the MyClass1.php and MyClass2.php files respectively.

The code is as follows:

Function _ autoload ($ class_name ){
Require_once $ class_name. '. php ';
}

$ Obj = new MyClass1 ();
$ Obj2 = new MyClass2 ();
?>

Before the magic function _ autoload () method appears, if you want to instantiate 100 objects in a program file, you must include or require 100 class files, or you can define these 100 classes in the same class file-I believe this file will be very large.
But the _ autoload () method does not have to worry about it in the future. this class will automatically load the specified file before you instantiate the object.

The following example shows how to use the PHP magic function _ autoload.

The code is as follows:


// Define a class ClassA named ClassA. php
Class ClassA {
Public function _ construct (){
Echo "ClassA load success! ";
}
}

// Define a class ClassB. the file name is ClassB. php, and ClassB inherits ClassA
Class ClassB extends ClassA {
Public function _ construct (){
// Parent ::__ construct ();
Echo "ClassB load success! ";
}
}

After defining the classes used for testing, we compile a PHP running program file containing the _ autoload () method as follows:

The code is as follows:

Function _ autoload ($ classname ){
$ Classpath = "./". $ classname. '. php ';
If (file_exists ($ classpath )){
Require_once ($ classpath );
}
Else {
Echo 'class file'. $ classpath. 'not found! ';
}
}

$ Newobj = new ClassA ();
$ Newobj = new ClassB ();

There is no problem with running this file. we can see how easy it is to use autoload ......
But I have to remind you that you must pay attention to several aspects.

1. if the class has an inheritance relationship (for example, ClassB extends ClassA), and ClassA is not in the directory of ClassB
A fatal error occurs when ClassB is instantiated using the _ autoload magic function:
Fatal error: Class 'classd' not found in ...... ClassB. php on line 2,

Solution: put all classes with extends relationships in the same file directory, or manually include the inherited classes in the file when instantiating an inheritance class;

2. Note that the class name and class file name must be consistent to facilitate the use of magic function _ autoload;

Other things to be aware:
3. this method is invalid when you run the PHP script in CLI mode;

4. if your class name is related to user input -- or dependent on user input, check the input file name, for example :.. /. /Such a file name is very dangerous.


_ Autoload

_ Autoload magic method or you are willing to call it a magic function, which is too specific. When he loads the class files to be included, he does not even need to declare anything other than the class defined in the class files.

Start to play back this mechanism.

First, create a Test. class. php file and type the following content:

$ PublicPara = 'When did the 17th CPC National Congress be held? ';

The code is as follows:
Class Test {
Public function _ construct (){
Global $ publicPara;
If (isset ($ publicPara )){
Echo $ publicPara;
}
Else {
Echo "what's my problem? ";
}
}
}

Remember to save this file!

Create a new file named do. php and type the following content:

The code is as follows:
Require_once ('test. class. php ');
New Test ();

In this case, the output is as expected: When did the 17th CPC National Congress be held?

But when you use the _ autoload magic method, the problem arises.

The code is as follows:
Function _ autoload ($ classname ){
Require_once ($ classname. ". class. php ");
}
New Test ();

This input is: What's the matter with me?

Obviously, he ignores the variables we define outside the class, that is, he only loads the classes we need to serialize, regardless of other declarations, and just other declarations, everything like echo is still executed.

...

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.