好!欢迎访问迷津渡口 现在时间是: 天气 °C ~ °C

迷津渡口

越感到迷惑 越接近真理

聚合类和深度克隆

聚合类是指在类中以某种方式嵌入了其他的类。

可以利用魔术方法__call访问原始的类方法,也可以直接定义相应的方法。

<?php

class Address {
    protected $city;
    protected $country;

    public function setCity($city) {$this->city=$city;}
    public function getCity() {return $this->city;}
    public function setCountry($country) {$this->country=$country;}
    public function getCountry(){return $this->country;}
}

class Person {
    protected $name;
    protected $address;

    public function __construct() {$this->address=new Address;}
    public function setName($name) {$this->name=$name;}
    public function getName() {return $this->name;}
    public function __call($method,$arguments) {
        if (method_exists($this->address,$method)) {
            return call_user_func_array(array($this->address,$method),$arguments);
        }
    }
}

$rasmus=new Person;
$rasmus->setName('Rasmus Lerdorf');
$rasmus->setCity('Sunnyvale');

$zeev=clone $rasmus;
$zeev->setName('Zeev Suraski');
$zeev->setCity('Tel Aviv');

print $rasmus->getName().' lives in '.$rasmus->getCity().".<br>";
print $zeev->getName().' lives in '.$zeev->getCity().".<br>";

在这个例子中,clone仅仅是默认的浅拷贝,$name可以覆盖,但$address需要明确的克隆才可以。

输出:

Rasmus Lerdorf lives in Tel Aviv.
Zeev Suraski lives in Tel Aviv.

在聚合类中实现深度克隆:

class Person {
    // ... 之前的所有代码
    public function __clone() {
        $this->address=clone $this->address;
    }
}

添加后,将实现深度克隆,所有属性都可以覆盖包含$address。

因此添加后的输出为:

Rasmus Lerdorf lives in Sunnyvale.
Zeev Suraski lives in Tel Aviv.


点赞1
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
ahtantalya
 ahtantalya 约 3 小时前回复
#193
<a href="http://www.awassessment.com/vintage-polo-by-ralph-lauren-overdyed-rugby-shirt-poloh">vintage polo by ralph lauren overdyed rugby shirt</a> <a href="http://www.websiteopmaat.com/nike-magista-orden-orange-shoesi">nike magista orden orange</a> <a href="http://www.lopifitfl.com/nike-air-max-97-blu-scarpej">nike air max 97 blu</a> <a href="http://www.aurorefleurs.com/ua-clutchfit-prodigy-m%C3%A4nner-blau-schwarz-schuhej">ua clutchfit prodigy m盲nner blau schwarz</a> <a href="http://www.ezedcal.com/white-sox-hat-chicago-flag-hath">white sox hat chicago flag</a> <a href="http://www.calsportswear.com/nike-air-max-2017-hommes-bleu-noir-chaussuresh">nike air max 2017 hommes bleu noir</a>
ahtantalya http://www.ahtantalya.com/
colintilley
 colintilley 2019-05-17 18:18回复
#192
<a href="http://www.atrapaniapp.com/nba-store-stephen-curry-jersey-nfli">nba store stephen curry jersey</a> <a href="http://www.maplebeyond.com/adidas-zx-750-herre-appelsin-skoq">adidas zx 750 herre appelsin</a> <a href="http://www.collisionsix.com/red-black-white-polo-shirt-poloh">red black white polo shirt</a> <a href="http://www.todololitas.com/nike-air-max-1-womens-black-silver-shoesi">nike air max 1 womens black silver</a> <a href="http://www.jcwjrlaw.com/womens-nike-flynit-air-max-yellow-black-shoesk">womens nike flynit air max yellow black</a> <a href="http://www.vstsblog.com/mennns-nike-air-max-90-vt-gul-gr%C3%A5-obuvq">mennns nike air max 90 vt gul gr氓</a>
colintilley http://www.colintilley.com/
ikmsdkravmaga
 ikmsdkravmaga 2019-05-17 18:09回复
#191
<a href="http://www.mandydugan.com/detroit-lions-new-era-2018-nfl-draft-low-profile-59fifty-cap-hath">detroit lions new era 2018 nfl draft low profile 59fifty cap</a> <a href="http://www.dxnkofe.com/air-jordan-retro-1-femmes-vert-or-chaussuresh">air jordan retro 1 femmes vert or</a> <a href="http://www.yurasmirnov.com/femmes-asics-gel-kayano-20-noir-or-chaussuresj">femmes asics gel kayano 20 noir or</a> <a href="http://www.blovese.com/air-jordan-retro-14-blau-orangen-schuhej">air jordan retro 14 blau orangen</a> <a href="http://www.ztsurfboards.com/nike-free-run-3-kvinders-bl%C3%A5-s%C3%B8lv-skok">nike free run 3 kvinders bl氓 s酶lv</a> <a href="http://www.visheshcap.com/maschio-asics-gel-noosa-tri-9-blu-viola-scarpeh">maschio asics gel noosa tri 9 blu viola</a>
ikmsdkravmaga http://www.ikmsdkravmaga.com/
asksohbetim
 asksohbetim 2019-05-17 00:47回复
#190
<a href="http://www.gibbsorganic.com/hombres-nike-free-run-4.0-verde-gris-skoe">hombres nike free run 4.0 verde gris</a> <a href="http://www.malatyapi.com/mike-trout-youth-jersey-red-nflh">mike trout youth jersey red</a> <a href="http://www.calwhitson.com/86-teuvo-teravainen-jersey-amazon-nfli">86 teuvo teravainen jersey amazon</a> <a href="http://www.glamourspausa.com/russell-westbrook-sonics-jersey-nflh">russell westbrook sonics jersey</a> <a href="http://www.thebeadpro.com/kvinders-nike-free-run-4.0-lilla-gr%C3%B8n-shoese">kvinders nike free run 4.0 lilla gr酶n</a> <a href="http://www.mollynurse.com/polo-ralph-lauren-plaid-oxford-shirt-poloh">polo ralph lauren plaid oxford shirt</a>
asksohbetim http://www.asksohbetim.com/
larastart
 larastart 2019-05-16 08:16回复
#189
<a href="http://www.ciaodearborn.com/femmes-air-max-90-hyperfuse-blanc-or-runningc">femmes air max 90 hyperfuse blanc or</a> <a href="http://www.ilrione.com/girls-p%C3%BArpura-high-top-converse-zapatosj">girls p煤rpura high top converse</a> <a href="http://www.bmthtaxis.com/nike-air-max-1-essential-chilling-rosso-chaussuresa">nike air max 1 essential chilling rosso</a> <a href="http://www.delmotkrt.com/nike-hypervenom-phantom-blanco-and-verde-foota">nike hypervenom phantom blanco and verde</a> <a href="http://www.emutemusic.com/nike-roshe-run-soft-grigio-mid-fog-runningd">nike roshe run soft grigio mid fog</a> <a href="http://www.kinofrost.com/nike-new-england-patriots-salute-to-service-ko-pullover-performance-hoodie-olive-nflh">nike new england patriots salute to service ko pullover performance hoodie olive</a>
[url=http://www.larastart.com/]larastart[/url]
notmyrichie
 notmyrichie 2019-05-16 07:53回复
#188
<a href="http://www.jupickuplines.com/femminile-nike-zoom-pegasus-32-tutti-rosso-scarpek">femminile nike zoom pegasus 32 tutti rosso</a> <a href="http://www.saleuggboot.com/femmes-nike-roshe-run-floral-oranger-noir-chaussuresh">femmes nike roshe run floral oranger noir</a> <a href="http://www.fragmanizletv.com/prada-saffiano-mini-galleria-crossbody-bag-pradah">prada saffiano mini galleria crossbody bag</a> <a href="http://www.swisshomeas.com/st-louis-cardinals-matt-carpenter-jersey-nfli">st louis cardinals matt carpenter jersey</a> <a href="http://www.marinersdiary.com/herre-jordan-trunner-dominie-pro-bl%C3%A5-skol">herre jordan trunner dominie pro bl氓</a> <a href="http://www.bourseasie.com/long-sleeve-ralph-lauren-polo-shirts-for-men-poloh">long sleeve ralph lauren polo shirts for men</a>
notmyrichie http://www.notmyrichie.com/
bagintaiwan
 bagintaiwan 2019-05-14 08:45回复
#187
<a href="http://www.michiganev.com/nike-air-max-90-premium-svart-hvit-gum-obuvi">nike air max 90 premium svart hvit gum</a> <a href="http://www.serdarhan.com/nike-shox-rivalry-2-all-black-shoesj">nike shox rivalry 2 all black</a> <a href="http://www.ankaralimnet.com/herre-air-jordan-retro-12-orange-shoesl">herre air jordan retro 12 orange</a> <a href="http://www.greenwichua.com/polo-v-neck-t-poloh">polo v neck t</a> <a href="http://www.minicomposer.com/converse-low-gr%C3%B8n-skok">converse low gr酶n</a> <a href="http://www.tabletopsport.com/mizuno-rosa-laufen-schuhe-schuheh">mizuno rosa laufen schuhe</a>
bagintaiwan http://www.bagintaiwan.com/
rastadread
 rastadread 2019-05-14 06:14回复
#186
<a href="http://www.hopitalhayek.com/nike-hypershift-rot-wei%C3%9F-schuhei">nike hypershift rot wei脽</a> <a href="http://www.cheapartsale.com/adidas-eqt-support-adv-gr%C3%A5-r%C3%B8d-hvit-obuvj">adidas eqt support adv gr氓 r酶d hvit</a> <a href="http://www.xjdscnuh.com/nike-air-max-2018-pelle-marrone-nero-scarpel">nike air max 2018 pelle marrone nero</a> <a href="http://www.cybcarusa.com/noir-chuck-taylor-converse-vente-chaussuresh">noir chuck taylor converse vente</a> <a href="http://www.drfoxxdds.com/nike-air-max-tavas-dark-grigio-scarpeh">nike air max tavas dark grigio</a> <a href="http://www.hagemannlaw.com/miami-dolphins-white-jersey-nfli">miami dolphins white jersey</a>
rastadread http://www.rastadread.com/
truenthusiast
 truenthusiast 2019-05-14 04:54回复
#185
<a href="http://www.emanmoon.com/prada-mens-sneaker-shoes-pradar">prada mens sneaker shoes</a> <a href="http://www.mysiatravel.com/t%C3%AAnis-nike-shox-superfly-r4-skos">t锚nis nike shox superfly r4</a> <a href="http://www.pandoraboxhn.com/ogando-nefi-44-jersey-youth-nflc">ogando nefi 44 jersey youth</a> <a href="http://www.yourctt.com/nike-kobe-8-mens-yellow-red-skoa">nike kobe 8 mens yellow red</a> <a href="http://www.coccodesigns.com/burberry-leopard-scarf-scarfr">burberry leopard scarf</a> <a href="http://www.lucasdow.com/black-pink-womens-nike-kobe-11-shoes-sneakerd">black pink womens nike kobe 11 shoes</a>
truenthusiast http://www.truenthusiast.com/
mamatale
 mamatale 2019-05-13 17:09回复
#184
<a href="http://www.lvnailbar.com/nike-air-max-90-prm-em-red-black-casual-shoesk">nike air max 90 prm em red black casual</a> <a href="http://www.eisenhards.com/ray-ban-wayfarer-black-and-white-frame-sunglassesh">ray ban wayfarer black and white frame</a> <a href="http://www.martinmakeup.com/hommes-nike-air-max-tn-du-vin-violet-chaussuresl">hommes nike air max tn du vin violet</a> <a href="http://www.dgrabovsky.com/nike-free-noir-chaussuresl">nike free noir</a> <a href="http://www.madeinmyindia.com/nike-roshe-frau-orange-gelb-schuhej">nike roshe frau orange gelb</a> <a href="http://www.manglammovers.com/adidas-zx-750-m%C3%A4nner-gelb-blau-wei%C3%9F-schuhei">adidas zx 750 m盲nner gelb blau wei脽</a>
mamatale http://www.mamatale.com/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸