Heim >Backend-Entwicklung >PHP-Tutorial >学习PHP之动态实例化类
Test1.class.php
<?php class Test1{ public function __construct(){ echo "Test1<br>"; } }
Test2.class.php
<?php class Test2{ public function __construct(){ echo "Test2<br>"; } }
实例化
<?php function loadclass($classname){ $filename = "./".$classname.".class.php"; if(is_file($filename)){ include $filename; } } spl_autoload_register("loadclass"); //实例化方法一 $class1 = "Test1"; if(class_exists($class1))//判断类是否存在 new $class1(); //实例化方法二 $class2 = "Test2"; $class2 = new ReflectionClass($class2);// 建立类的反射 $instance = $class2 ->newInstance();// 相当于实例化类