//加载类: include("./Ren.class.php"); include "./Ren.class.php"; require("./Ren.class.php"); require "./Ren.class.php"; require_once("./Ren.class.php"); require_once "./Ren.class.php"; //自动加载类的方法 //所有的类文件要放在同一个目录下 //所有类文件的命名规则一致 function __autoload($classname) { require $classname.".class.php"; } $r = new Ren(); //设计模式 //1.单例模式 //类的计划生育 //1.让该类在外界无法造对象 //2.让外界可以造一个对象,做一个静态方法返回对象 //3.在类里面通过静态变量控制返回对象只能是一个 class Dog { static $dx; public $test; private function __construct() { } static function DuiXiang() { //return new Dog(); if(empty(self::$dx)) { self::$dx = new Dog(); } return self::$dx; } } $a = Dog::DuiXiang(); $b = Dog::DuiXiang(); $b->test="hello"; var_dump($a); //工厂模式 class YunSuan { public $a; public $b; function Jia() { return $a+$b; } function Jian() { return $a-$b; } } abstract class YuanSuan { public $a; public $b; function Suan() { } } class Jia extends YuanSuan { function Suan() { return $this->a+$this->b; } } class Jian extends YuanSuan { function Suan() { return $this->a-$this->b; } } class Cheng extends YuanSuan { function Suan() { return $this->a*$this->b; } } $j = new Cheng(); $j->Suan(); class GongChang { static function ShengChan($f) { switch($f) { case "+": return new Jia(); break; case "-": return new Jian(); break; case "*": return new Cheng(); break; } } } $r = GongChang::ShengChan("*"); $r->a=10; $r->b=5; echo $r->Suan(); ?>