Home > Backend Development > PHP Tutorial > About loading classes in PHP

About loading classes in PHP

autoload
Release: 2023-04-09 18:56:01
Original
3170 people have browsed it

Object-oriented is an important idea, and classes are also important concepts in object-oriented, but class loading is the key to using classes.

There are two ways to access classes:

  • Access through instantiated objects

  • Access to class members

The prerequisite for access is that there is a class in the memory, so the class needs to be loaded into the memory in advance.

1. Manual loading

//类文件 Salary.php
<?php
   class Salary{
      public  function Student(){
        echo "Salary下面的Student方法";
      }
   }
?>
Copy after login
应用文件:useSalary.php
<?php
//$s = new Salary();会报错,因为useSalaty.php中间未定义Salary类

include_once &#39;Salary.php&#39;;				//也可以使用require,通常使用_once,因为类不允许重名
$s = new Salary();
echo $s->Student();
?>
Copy after login
<?php
//加载类文件是一种比较消耗资源的方式,可以事先使用class_exists()函数来判定类是否存在,存在就不用加载,不存在才加载
if(!class_exists(&#39;Salary&#39;)){
	//不存在:加载
    include_once &#39;Salary.php&#39;;
}
//使用
$s = new Salary();
?>
Copy after login

2. Automatic loading

  • The automatic loading mechanism used before PHP7: Use the __autoload() function provided by the system. Then when the system needs to use the class and it does not exist in the memory, the system will automatically call the __autoload() function to load the class file. .

<?php
function __autoload($classname){	//参数为类名:即当前需要访问的类的名字
//需要人为定义去哪加载,怎么加载   
    include_once $classname . &#39;.php&#39;;	//假定为当前目录下,类文件名字为:类名.php
}

//使用类:内存目前并没有
$s = new Salary();	//系统发现内存没有Salary,所以调用__autoload()去加载
?>
Copy after login
//若在不同路径下
<?php
//定义自动加载
function __autoload($classname){
    $abc_file = &#39;abc/&#39; . $classname . &#39;.php&#39;;		//如abc/Salary.php
    if(file_exists($c_file)){                      //利用file_exists()判断文件是否存在
        include_once $abc_file;
    }
}
?>
Copy after login
  • After PHP7, it is not recommended to use the __autoload() function, but to use a registration mechanism to customize the user The function is placed inside the system and uses spl_autoload_register (defined function).

<?php
function myself_autoload($classname){		//与__autoload()类似
    $abc_file = &#39;abc/&#39; . $classname . &#39;.php&#39;;		//如abc/Salary.php
    if(file_exists($c_file)){
        include_once $c_file;
    }
}
//此时,上述函数永远不会自动运行,除非将函数注册到系统内部
spl_autoload_register(&#39;myself_autoload&#39;);
?>
Copy after login
//可以定义多个方法
<?php
function wayone_autoload($classname){		
   
function waytwo_autoload($classname){		
    
}
//此时,上述函数永远不会自动运行,除非将函数注册到系统内部
spl_autoload_register(&#39;wayone_autoload&#39;);
spl_autoload_register(&#39;waytwo_autoload&#39;);
?>
Copy after login

Recommended: php tutorial

The above is the detailed content of About loading classes in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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