Home >Backend Development >PHP Tutorial >学习PHP之动态实例化类

学习PHP之动态实例化类

WBOY
WBOYOriginal
2016-06-23 13:27:031635browse

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();// 相当于实例化类
Statement:
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