在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
加载类: include("./Ren.class.php"); include "./Ren.class.php"; 用此方法的时候include后面要加空格 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.让该类在外界无法造对象 2.让外界可以造一个对象,做一个静态方法返回对象 3.在类里面通过静态变量控制返回对象只能是一个 class Dog { static $dx; public $test; private function __construct() { } static function DuiXiang() { //return new Dog(); if(empty(self::$dx)) 静态要加self { 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();
|
2022-08-17
2022-11-06
2022-08-17
2022-07-29
2022-07-29
请发表评论