设计模式
1.单例模式
类的计划生育
1.让该类在外界无法造对象
2.让外界可以造一个对象,做一个静态方法返回对象
3.在类里面通过静态变量控制返回对象只能是一个
<?php class Dog { static $dx; public $test; private function __construct() { } static function duixiang() { if(empty(self::$dx)) { self::$dx=new Dog(); } return self::$dx; } } $a=Dog::duixiang(); $b=Dog::duixiang(); $b->test="素质教育"; var_dump($b); class Yunsuan { public $a; public $b; function jia() { return $this->a+$this->b; } function jian() { return $this->a+ $this->b; } } $r=new Yunsuan(); $r->a=10; $r->b=5; echo $r->jian(); abstract class yunsuan //抽象类 { public $a; public $b; function suan() { } } class jia extends yunsuan { function suan() { return $this->a+$this->b; } } class jian extends yunsuan { function suan () { return $this->a-$this->b; } } class cheng extends yunsuan { function suan() { return $this->a*$this->b; } } /*$j= new jia(); $j->a=10; $j->b=7; echo $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=99999; $r->b=34567; echo $r->suan();
时间: 2024-10-07 06:32:59