学习PHP之动态实例化类

WBOY
Release: 2016-06-23 13:27:03
Original
1574 people have browsed it

Test1.class.php

<?php        class Test1{            public function __construct(){                    echo "Test1<br>";                }        }
Copy after login

Test2.class.php

<?php        class Test2{              public function __construct(){                    echo "Test2<br>";              }      }
Copy after login

实例化

<?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();// 相当于实例化类
Copy after login
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!