default.php:
<?php namespace SiteInfo{ class Site{ var $url; var $title; 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; } } } ?>
index.php:
<?php namespace DoSomething{ require ('default.php');//require只是引用一次,報錯后就中終斷執行,include是每次加載都執行,報錯后只是提示,后續程序繼續執行 use SiteInfo\Site;//引用命名空間\類名 class myClass{ function getSite(){ $taobao=new Site();//實例化方法,這些實例化只能寫到類的方法里面不能再類里面實例化 $taobao->setUrl('hello word~~!!!!!!!@!!!!@#$%YUI'); $result = $taobao->getUrl(); echo $result; } } $myClass = new myClass(); $myClass->getSite();//類方法調用 } ?>