好!欢迎访问迷津渡口 现在时间是: 天气 °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
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
slotoyunpro
 slotoyunpro 约 6 小时前回复
#98
<a href="http://www.lvnailbar.com/nike-hypershift-grau-schuhei">nike hypershift grau</a> <a href="http://www.eisenhards.com/nike-free-5.0-tr-fit-3-m%C3%A6nd-royal-bl%C3%A5-orange-skoh">nike free 5.0 tr fit 3 m忙nd royal bl氓 orange</a> <a href="http://www.martinmakeup.com/mlb-jerseys-houston-astros-17-lance-berkman-red-jerseys-nfli">mlb jerseys houston astros 17 lance berkman red jerseys</a> <a href="http://www.dgrabovsky.com/where-to-kj%C3%B8pe-svart-high-top-converse-obuvk">where to kj酶pe svart high top converse</a> <a href="http://www.madeinmyindia.com/coach-velcro-shoes-coachh">coach velcro shoes</a> <a href="http://www.manglammovers.com/nike-shox-current-bianca-rosa-grigio-scarpej">nike shox current bianca rosa grigio</a>
slotoyunpro http://www.slotoyunpro.com/
ispardmore
 ispardmore 2019-03-18 03:29回复
#97
<a href="http://www.snifferwalk.com/nike-zoom-pegasus-34-gold-gr%C3%BCn-schuhej">nike zoom pegasus 34 gold gr眉n</a> <a href="http://www.fishmackerel.com/kvinders-nike-shox-turbo-21-bl%C3%A5-gul-skop">kvinders nike shox turbo 21 bl氓 gul</a> <a href="http://www.lauraenergy.com/nike-kyrie-1-verde-bianca-scarpeh">nike kyrie 1 verde bianca</a> <a href="http://www.magellantt.com/nike-blazer-bianca-grigio-scarpei">nike blazer bianca grigio</a> <a href="http://www.sportablecrew.com/2001-02-marty-biron-buffalo-sabres-game-worn-jersey--alternate-photo-match-nflh">2001 02 marty biron buffalo sabres game worn jersey  alternate photo match</a> <a href="http://www.digisolnc.com/coach-hc7068-sunglasses-coachh">coach hc7068 sunglasses</a>
ispardmore http://www.ispardmore.com/
drpaulragas
 drpaulragas 2019-03-17 02:06回复
#96
<a href="http://www.asinagaojisan.com/kvinders-nike-air-max-tailwind-7-l%C3%B8b-sko-herre-butya">kvinders nike air max tailwind 7 l酶b sko herre</a> <a href="http://www.captmollie.com/nike-womens-nevin-lawson-black-backer-detroit-lions-nfl-24-pullover-hoodie-nflu">nike womens nevin lawson black backer detroit lions nfl 24 pullover hoodie</a> <a href="http://www.centrodunamis.com/air-jordan-1-triple-negro-amarillo-kurpesb">air jordan 1 triple negro amarillo</a> <a href="http://www.diendraht.com/nike-lunar-tempo-negro-plata-schuhec">nike lunar tempo negro plata</a> <a href="http://www.hazalyildirim.com/flyknit-trainer-bleu-glow-plug-schuheg">flyknit trainer bleu glow plug</a> <a href="http://www.rivacover.com/dam%C3%A4nner-air-jordan-retro-2-gold-wei%C3%9F-kurpesc">dam盲nner air jordan retro 2 gold wei脽</a>
drpaulragas http://www.drpaulragas.com/
pariswebjob
 pariswebjob 2019-03-16 10:12回复
#95
<a href="http://www.setusoftware.com/kj%C3%B8pe-hvit-nike-huarache-obuvk">kj酶pe hvit nike huarache</a> <a href="http://www.shopteamcubs.com/nike-air-max-2016-grigio-fly-scarpei">nike air max 2016 grigio fly</a> <a href="http://www.danieldavinci.com/mujeres-air-jordan-retro-11-blanco-zapatosh">mujeres air jordan retro 11 blanco</a> <a href="http://www.lovirani.com/nike-hyperdunk-2016-hvid-sort-skok">nike hyperdunk 2016 hvid sort</a> <a href="http://www.rosebites.com/nike-free-run-4.0-all-svart-obuvi">nike free run 4.0 all svart</a> <a href="http://www.webrushllc.com/hombres-mizuno-wave-prophecy-5-negro-verde-zapatosl">hombres mizuno wave prophecy 5 negro verde</a>
pariswebjob http://www.pariswebjob.com/
allmydibs
 allmydibs 2019-03-16 08:29回复
#94
<a href="http://www.seesoshow.com/hombres-nike-free-run-3-blanco-zapatosh">hombres nike free run 3 blanco</a> <a href="http://www.crypolio.com/nike-air-max-90-mujer-wholeventa-marr%C3%B3n-blanco-azul-zapatosk">nike air max 90 mujer wholeventa marr贸n blanco azul</a> <a href="http://www.vtrvideo.com/north-face-polartec-denali-fleece-northfaceh">north face polartec denali fleece</a> <a href="http://www.asoseinhalb.com/adidas-pure-boost-2-schwarz-schuhek">adidas pure boost 2 schwarz</a> <a href="http://www.handydgh.com/gr%C3%B8n-converse-sko-skoi">gr酶n converse sko</a> <a href="http://www.blakefanz.com/converse-jaune-low-chaussuresh">converse jaune low</a>
allmydibs http://www.allmydibs.com/
nike roshe dmb azul
 nike roshe dmb azul 2019-03-16 00:55回复
#93
<a href="http://www.cardiachits.com/womens-nike-air-max-thea-all-yellow-shoesi">womens nike air max thea all yellow</a> <a href="http://www.zestchannel.com/womens-asics-gel-noosa-tri-8-silver-white-shoesp">womens asics gel noosa tri 8 silver white</a> <a href="http://www.eczacinim.com/kvinners-nike-roshe-run-floral-oransje-obuvl">kvinners nike roshe run floral oransje</a> <a href="http://www.chilangorails.com/adidas-gazelle-boost-gr%C3%A5-guld-skoj">adidas gazelle boost gr氓 guld</a> <a href="http://www.mariusen.com/nike-air-max-nero-rosso-scarpel">nike air max nero rosso</a> <a href="http://www.brittonoilco.com/mens-nike-huarache-utility-blue-black-shoesj">mens nike huarache utility blue black</a>
nike roshe dmb azul http://www.xthbgc.com/nike-roshe-dmb-azul-zapatosq
breachurch
 breachurch 2019-03-15 19:37回复
#92
<a href="http://www.czappmasonry.com/nike-air-max-1-rose-gull-uk-schuheg">nike air max 1 rose gull uk</a> <a href="http://www.hiussalipiilo.com/air-jordan-11-sort-bl%C3%A5-schuheb">air jordan 11 sort bl氓</a> <a href="http://www.olgroupusainc.com/nike-free-run-4.0-rot-ink-chaussuresd">nike free run 4.0 rot ink</a> <a href="http://www.shahsmkj.com/nike-lebron-12-bl%C3%A5-and-s%C3%B8lv-butyg">nike lebron 12 bl氓 and s酶lv</a> <a href="http://www.theuntamedco.com/kyrie-2-azul-zones-scarpea">kyrie 2 azul zones</a> <a href="http://www.wsendeavors.com/air-max-95-qs-guld-schuheb">air max 95 qs guld</a>
breachurch http://www.breachurch.com/
imagimp
 imagimp 2019-03-14 12:21回复
#91
<a href="http://www.teamdalarna.com/dam%C3%A4nner-asics-gel-noosa-tri-10-himmelblau-grau-schuhej">dam盲nner asics gel noosa tri 10 himmelblau grau</a> <a href="http://www.syairshamyoun.com/herre-nike-air-max-tailwind-6-gr%C3%A5-appelsin-skoh">herre nike air max tailwind 6 gr氓 appelsin</a> <a href="http://www.fizalisfresh.com/femmes-nike-air-huarache-nm-noir-or-chaussuresh">femmes nike air huarache nm noir or</a> <a href="http://www.fsscas.com/oklahoma-city-thunder-new-era-nba-color-prism-pack-59fifty-cap-hatl">oklahoma city thunder new era nba color prism pack 59fifty cap</a> <a href="http://www.imagimp.com/adidas-springblade-pro-gris-rose-chaussuresh">adidas springblade pro gris rose</a> <a href="http://www.burmamaritime.com/air-jordan-retro-29-hombresta-verde-p%C3%BArpura-zapatosp">air jordan retro 29 hombresta verde p煤rpura</a>
imagimp http://www.imagimp.com/
timowallets
 timowallets 2019-03-12 16:21回复
#90
<a href="http://www.matrawildlife.com/nike-magista-opus-ii-nero-scarpeh">nike magista opus ii nero</a> <a href="http://www.hiyawigs.com/houston-astros-rainbow-jersey-nfli">houston astros rainbow jersey</a> <a href="http://www.fidgefingers.com/hombres-under-armour-curry-one-fluorescent-verde-rojo-zapatosh">hombres under armour curry one fluorescent verde rojo</a> <a href="http://www.lintvan.com/nike-free-5.0-azul-negro-zapatosk">nike free 5.0 azul negro</a> <a href="http://www.pubgdust.com/north-face-womens-hyvent-jacket-waterproof-indigo-violet-northfaceh">north face womens hyvent jacket waterproof indigo violet</a> <a href="http://www.ahtantalya.com/nike-flyknit-triple-wei%C3%9F-schuhek">nike flyknit triple wei脽</a>
[url=http://www.timowallets.com/]timowallets[/url]
sotainvalidi
 sotainvalidi 2019-03-10 22:20回复
#89
<a href="http://www.foradocasulo.com/adidas-zx-flux-hvit-woven-scarpeg">adidas zx flux hvit woven</a> <a href="http://www.kcsanpedro.com/dam%C3%A4nner-asics-gel-kinsei-5-gr%C3%BCn-kurpese">dam盲nner asics gel kinsei 5 gr眉n</a> <a href="http://www.lordofvirtue.com/nike-kobe-10-grau-rosa-schuheg">nike kobe 10 grau rosa</a> <a href="http://www.thbdtv.com/free-run-2-marine-schuheb">free run 2 marine</a> <a href="http://www.easytanklevel.com/lebron-14-brouge-for-vente-chaussurese">lebron 14 brouge for vente</a> <a href="http://www.golfbreakuk.com/air-max-2016-prezzo-in-india-butyg">air max 2016 prezzo in india</a>
sotainvalidi http://www.sotainvalidi.com/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸