ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ <?php class Button{} class WinButton extends Button{} class MacButton extends Button{} interface ButtonFactory{ public function createButton($type); } class MyButtonFactory implements ButtonFactory{ public function createButton($type){ switch($type){ case 'win': return new WinButton(); case 'mac': return new MacButton(); } } } $button_obj = new MybuttonFactory(); var_dump($button_obj->createButton('win')); var_dump($button_obj->createButton('mac')); ~~~ 传递不同的参数值,获得不同的类对象 object(WinButton)#2 (0) { } object(MacButton)#2 (0) { }