您好,登錄后才能下訂單哦!
這篇文章主要介紹php創(chuàng)建新對(duì)象的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
php怎么創(chuàng)建新對(duì)象?
首先定義一個(gè)類;
然后在類創(chuàng)建后,我們可以使用 new 運(yùn)算符來實(shí)例化該類的對(duì)象;
完整代碼如下:
<?php class Site { /* 成員變量 */ var $url; var $title; /* 成員函數(shù) */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url . PHP_EOL; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . PHP_EOL; } } $php = new Site; $taobao = new Site; $google = new Site; // 調(diào)用成員函數(shù),設(shè)置標(biāo)題和URL $php->setTitle( "億速云" ); $taobao->setTitle( "淘寶" ); $google->setTitle( "Google 搜索" ); $php->setUrl( 'www.php.cn' ); $taobao->setUrl( 'www.taobao.com' ); $google->setUrl( 'www.google.com' ); // 調(diào)用成員函數(shù),獲取標(biāo)題和URL $php->getTitle(); $taobao->getTitle(); $google->getTitle(); $php->getUrl(); $taobao->getUrl(); $google->getUrl(); ?>
以上是php創(chuàng)建新對(duì)象的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。