PHP面向对象程序设计之对象生成方法详解
程序员文章站
2024-04-01 22:06:10
本文实例讲述了php面向对象程序设计之对象生成方法。分享给大家供大家参考,具体如下:
对象
看个例子
本文实例讲述了php面向对象程序设计之对象生成方法。分享给大家供大家参考,具体如下:
对象
看个例子
<?php abstract class employee { // 雇员 protected $name; function __construct( $name ) { $this->name = $name; } abstract function fire(); } class minion extends employee { // 奴隶 继承 雇员 function fire() { print "{$this->name}: i'll clear my desk\n"; } } class nastyboss { // 坏老板 private $employees = array(); function addemployee( $employeename ) { // 添加员工 $this->employees[] = new minion( $employeename ); // 代码灵活性受到限制 } function projectfails() { if ( count( $this->employees ) > 0 ) { $emp = array_pop( $this->employees ); $emp->fire(); // 炒鱿鱼 } } } $boss = new nastyboss(); $boss->addemployee( "harry" ); $boss->addemployee( "bob" ); $boss->addemployee( "mary" ); $boss->projectfails(); // output: // mary: i'll clear my desk ?>
再看一个更具有灵活性的案例
<?php abstract class employee { protected $name; function __construct( $name ) { $this->name = $name; } abstract function fire(); } class minion extends employee { function fire() { print "{$this->name}: i'll clear my desk\n"; } } class nastyboss { private $employees = array(); function addemployee( employee $employee ) { // 传入对象 $this->employees[] = $employee; } function projectfails() { if ( count( $this->employees ) ) { $emp = array_pop( $this->employees ); $emp->fire(); } } } // new employee class... class cluedup extends employee { function fire() { print "{$this->name}: i'll call my lawyer\n"; } } $boss = new nastyboss(); $boss->addemployee( new minion( "harry" ) ); // 直接以对象作为参数,更具有灵活性 $boss->addemployee( new cluedup( "bob" ) ); $boss->addemployee( new minion( "mary" ) ); $boss->projectfails(); $boss->projectfails(); $boss->projectfails(); // output: // mary: i'll clear my desk // bob: i'll call my lawyer // harry: i'll clear my desk ?>
单例
<?php class preferences { private $props = array(); private static $instance; // 私有的,静态属性 private function __construct() { } // 无法实例化,私有的构造函数 public static function getinstance() { // 返回对象 静态方法才可以被类访问,静态方法中要有静态属性 if ( empty( self::$instance ) ) { self::$instance = new preferences(); } return self::$instance; } public function setproperty( $key, $val ) { $this->props[$key] = $val; } public function getproperty( $key ) { return $this->props[$key]; } } $pref = preferences::getinstance(); $pref->setproperty( "name", "matt" ); unset( $pref ); // remove the reference $pref2 = preferences::getinstance(); print $pref2->getproperty( "name" ) ."\n"; // demonstrate value is not lost ?>
点评:不能随意创建对象,只能通过preferences::getinstance()来创建对象。
工厂模式
<?php abstract class apptencoder { abstract function encode(); } class bloggsapptencoder extends apptencoder { function encode() { return "appointment data encoded in bloggscal format\n"; } } class megaapptencoder extends apptencoder { function encode() { return "appointment data encoded in megacal format\n"; } } class commsmanager { // 负责生产bloggs对象 function getapptencoder() { return new bloggsapptencoder(); } } $obj = new commsmanager(); $bloggs = $obj->getapptencoder(); // 获取对象 print $bloggs->encode(); ?>
output:
appointment data encoded in bloggscal format
进一步增加灵活性设置
<?php abstract class apptencoder { abstract function encode(); } class bloggsapptencoder extends apptencoder { function encode() { return "appointment data encoded in bloggscal format\n"; } } class megaapptencoder extends apptencoder { function encode() { return "appointment data encoded in megacal format\n"; } } class commsmanager { const bloggs = 1; const mega = 2; private $mode ; function __construct( $mode ) { $this->mode = $mode; } function getheadertext() { switch ( $this->mode ) { case ( self::mega ): return "megacal header\n"; default: return "bloggscal header\n"; } } function getapptencoder() { switch ( $this->mode ) { case ( self::mega ): return new megaapptencoder(); default: return new bloggsapptencoder(); } } } $man = new commsmanager( commsmanager::mega ); print ( get_class( $man->getapptencoder() ) )."\n"; $man = new commsmanager( commsmanager::bloggs ); print ( get_class( $man->getapptencoder() ) )."\n"; ?>
output:
megaapptencoder
bloggsapptencoder
工厂方法模式要把创建者类与要生产的产品类分离开来。
抽象工厂
通过抽象来来约束,成为一定的规矩。
<?php abstract class apptencoder { abstract function encode(); } class bloggsapptencoder extends apptencoder { function encode() { return "appointment data encoded in bloggscal format\n"; } } class megaapptencoder extends apptencoder { function encode() { return "appointment data encoded in megacal format\n"; } } abstract class commsmanager { // 预约 abstract function getheadertext(); abstract function getapptencoder(); abstract function getttdencoder(); abstract function getcontactencoder(); abstract function getfootertext(); } class bloggscommsmanager extends commsmanager { function getheadertext() { return "bloggscal header\n"; } function getapptencoder() { return new bloggsapptencoder(); } function getttdencoder() { return new bloggsttdencoder(); } function getcontactencoder() { return new bloggscontactencoder(); } function getfootertext() { return "bloggscal footer\n"; } } class megacommsmanager extends commsmanager { function getheadertext() { return "megacal header\n"; } function getapptencoder() { return new megaapptencoder(); } function getttdencoder() { return new megattdencoder(); } function getcontactencoder() { return new megacontactencoder(); } function getfootertext() { return "megacal footer\n"; } } $mgr = new megacommsmanager(); print $mgr->getheadertext(); print $mgr->getapptencoder()->encode(); // 对象调用方法,返回对象,继续调用方法。 print $mgr->getfootertext(); ?>
output:
megacal header
appointment data encoded in megacal format
megacal footer
更加牛逼的实现
<?php // 根据类图,规划类的代码。从大局入手。 abstract class apptencoder { abstract function encode(); } class bloggsapptencoder extends apptencoder { function encode() { return "appointment data encoded in bloggscal format\n"; } } class megaapptencoder extends apptencoder { function encode() { return "appointment data encoded in megacal format\n"; } } abstract class commsmanager { const appt = 1; const ttd = 2; const contact = 3; abstract function getheadertext(); abstract function make( $flag_int ); // int标记 abstract function getfootertext(); } class bloggscommsmanager extends commsmanager { function getheadertext() { return "bloggscal header\n"; } function make( $flag_int ) { switch ( $flag_int ) { case self::appt: // self直接控制常量 return new bloggsapptencoder(); case self::contact: return new bloggscontactencoder(); case self::ttd: return new bloggsttdencoder(); } } function getfootertext() { return "bloggscal footer\n"; } } $mgr = new bloggscommsmanager(); print $mgr->getheadertext(); print $mgr->make( commsmanager::appt )->encode(); print $mgr->getfootertext(); ?>
output:
bloggscal header
appointment data encoded in bloggscal format
bloggscal footer
原型模式
改造成一个保存具体产品的工厂类。
<?php class sea {} // 大海 class earthsea extends sea {} class marssea extends sea {} class plains {} // 平原 class earthplains extends plains {} class marsplains extends plains {} class forest {} // 森林 class earthforest extends forest {} class marsforest extends forest {} class terrainfactory { // 地域工厂 private $sea; private $forest; private $plains; function __construct( sea $sea, plains $plains, forest $forest ) { // 定义变量为类对象 $this->sea = $sea; $this->plains = $plains; $this->forest = $forest; } function getsea( ) { return clone $this->sea; // 克隆 } function getplains( ) { return clone $this->plains; } function getforest( ) { return clone $this->forest; } } $factory = new terrainfactory( new earthsea(), new earthplains(), new earthforest() ); print_r( $factory->getsea() ); print_r( $factory->getplains() ); print_r( $factory->getforest() ); ?>
output:
earthsea object ( ) earthplains object ( ) earthforest object ( )
更多关于php相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
上一篇: 谈谈php对接芝麻信用踩的坑