public static function main(array $args = array()) { $tea = new Tea(); $coffee = new Coffee(); println("Making tea..."); $tea->prepareRecipe(); println("Making coffee..."); $coffee->prepareRecipe(); }
<?php function __autoload($class_name) { require_once $class_name . '.php'; } $coffee = new Coffee(); $tea = new Tea(); $coffee->prepareRecipe(); $tea->prepareRecipe();
<?php ini_set('display_errors', "On"); error_reporting(E_ALL | E_STRICT); //自动加载类 set_include_path(get_include_path() . PATH_SEPARATOR . 'class/'); //设置加载路径 spl_autoload_extensions('.php'); //设置加载后缀名 function myAutoload($className) { require_once $className . '.php'; //直接根据类名跟文件关系包含文件 } spl_autoload_register("myAutoload"); //注册自动加载函数 //测试代码开始 $myTea = new Tea(); $myTea->prepareRecipe(); echo '<hr>'; echo file_get_contents('./read.txt');