PHP Magic function __autoload usage and some questions _php tutorial

Source: Internet
Author: User
Tags autoload what echo
This article describes a new function of php5, we introduce the use of PHP magic function __autoload and some problems, the following summarizes the use of the process of some problems and considerations.

__autoload () Usage

Some of the lectures in the PHP manual

Automatically load objects
Many developers write object-oriented applications to create a PHP source file for each class definition. A big annoyance is having to write a long list of included files at the beginning of each script (one file per class).

In PHP 5, this is no longer necessary. You can define a __autoload function that will be called automatically when you try to use a class that is not already defined. By calling this function, the scripting engine has the last chance to load the required classes before PHP fails.

Note:

Exceptions thrown in the __autoload function cannot be caught by a catch statement block and cause a fatal error.


Note:

If you use PHP's CLI interaction mode, autoloading does not exist.

Example #1 Autoload Example

This example attempts to load the MyClass1 and MyClass2 classes from the myclass1.php and myclass2.php files, respectively.

The code is as follows Copy Code

function __autoload ($class _name) {
Require_once $class _name. '. php ';
}

$obj = new MyClass1 ();
$obj 2 = new MyClass2 ();
?>

PHP before the Magic Function __autoload () method appears, if you want to instantiate 100 objects in a program file, then you have to include 100 class files with include or require, or you have to define the 100 classes in the same class file-- I believe this file will be very big.
But the __autoload () method comes out, and you don't have to worry about it later, and this class automatically loads the files you've created before you instantiate the object.

Let's take a look at an example of how to use this, and later explain what you should pay attention to using the PHP magic function __autoload.

The code is as follows Copy Code


Defines a class ClassA, with a file name of classa.php
Class classa{
Public Function __construct () {
echo "ClassA load success!";
}
}

Define a class ClassB with file name CLASSB.PHP,CLASSB inheritance ClassA
Class ClassB extends ClassA {
Public Function __construct () {
Parent::__construct ();
echo "ClassB load success!";
}
}

After defining the two test classes, let's write a PHP run program file containing the __autoload () method as follows:

The code is as follows Copy Code

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 ();

The operation of this file is a bit of a problem, it can be seen how good autoload ah, hehe ...
But I have to remind you that there are several aspects that must be noted.

1. If the class has an inheritance relationship (for example: ClassB extends ClassA), and ClassA is not in the directory where ClassB is located
Using the __autoload magic function to instantiate a CLASSB can be a fatal error:
Fatal error:class ' CLASSD ' not found in ... Classb.php on line 2,

Workaround: Place all classes that have extends relationships in the same file directory, or manually include the inherited classes in the file when instantiating an inheriting class;

2, another need to note is that the class name and the name of the class must be consistent, in order to more convenient use of magic function __autoload;

Other things to keep in mind:
3. This method is invalid when running PHP script in CLI mode;

4. If your class name is related to the user's input-or depends on the user's input, be sure to check the input file name, for example: /./such a file name is very dangerous.


__autoload's Problem

__autoload Magic method or you are willing to call him a magic function, too exclusive. When he loads a class file that needs to be included, even a declaration other than the class defined in the class file.

Start playing back the mechanism.

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

$publicPara = ' When was the Communist Party held in 17? ';

The code is as follows Copy Code
Class test{
Public Function __construct () {
Global $publicPara;
if (Isset ($publicPara)) {
Echo $publicPara;
}
else{
echo "What the hell is wrong with me?" ";
}
}
}

Remember to save this file Oh!

Then re-create a file named do.php and type the following

The code is as follows Copy Code
Require_once (' Test.class.php ');
New Test ();

So the output, as we expected, was: When was the Communist Party 17?

But when you use the Magic method of __autoload, the problem comes out.

The code is as follows Copy Code
function __autoload ($classname) {
Require_once ($classname. ". Class.php ");
}
New Test ();

This input is actually: what's the matter with me?

Obviously he ignores the variables we define outside of class, that is, he only loads the classes that we need to serialize, regardless of the other declarations, or just other declarations, such as what Echo does.

http://www.bkjia.com/PHPjc/632162.html www.bkjia.com true http://www.bkjia.com/PHPjc/632162.html techarticle This article describes a new function of php5, we introduce the use of PHP magic function __autoload and some problems, the following summarizes the use of the process of some problems and attention to things ...

  • Related Article

    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.