Home > Backend Development > PHP Tutorial > php __autoload magic method_PHP tutorial

php __autoload magic method_PHP tutorial

WBOY
Release: 2016-07-13 16:55:20
Original
873 people have browsed it

It seems that this function did not exist before php5. Now let’s take a look at the use of the new autoload magic method. When you understand it, you think it is so magical. Okay, without further ado, let’s take a look at the test filtering.

However, today I found that this __autoload magic method or you want to call it a magic function is too specific. When he loads the class file that needs to be included, it does not even care about other statements in the class file other than the class definition.

Start replaying this mechanic.

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

Then create a new file named do.php and type the following content
The code is as follows
 代码如下 复制代码

$publicPara='中共十七大啥时候召开的?';
class Test{
 public function  __construct(){
  global $publicPara;
  if(isset($publicPara)){
   echo $publicPara;
  }
  else{
   echo "管我啥事儿了?";
  }
 }
}

Copy code

$publicPara='When will the 17th National Congress of the Communist Party of China be held? ';
class Test{
public function __construct(){
global $publicPara;
if(isset($publicPara)){
echo $publicPara;
}
else{
echo "What do you care about me?";
}
}
}

 代码如下 复制代码
require_once('Test.class.php');
new Test();
?>
Remember

 代码如下 复制代码

function __autoload($classname){
require_once($classname.".class.php");
}
new Test();
?>

You have to save this file!

In this case, the output is just as we expected: When will the 17th National Congress of the Communist Party of China be held?

The code is as follows Copy code
function __autoload($classname){
require_once($classname.".class.php");
}
new Test();
?>
http://www.bkjia.com/PHPjc/631679.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631679.htmlTechArticleIt seems that this function did not exist before php5. Now let’s take a look at how to use the new autoload magic method. After you get to know him, you think he is amazing. Okay, without further ado, take a look...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template