本文实例讲述了PHP面向对象程序设计之命名空间与自动加载类。分享给大家供大家参考,具体如下:
命名空间
避免类名重复,而产生错误。
<"useful/Outputter.php"; class Outputter { // output data private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } $obj = new Outputter(); // 同一命名空间下,类名不能相同,默认命名空间为空。空也是一种命名空间。 $obj -> setName("Jack"); print $obj->getName(); //namespace useful; // 更改命名空间,否则查询不到Hello类,Fatal error: Class 'my\Hello' not found $hello = new Hello(); "color: #0000ff">如何调用命名空间中的类<"hello from Debug\n"; } } namespace main; // com\getinstance\util\Debug::helloWorld(); // 找不到Debug类 \com\getinstance\util\Debug::helloWorld(); // 加斜杠之后,就从根部去寻找了。 // outPut:hello from Debug "color: #0000ff">使用use关键字<"hello from Debug\n"; } } namespace main; use com\getinstance\util; //Debug::helloWorld(); //Fatal error: Class 'main\Debug' not found util\Debug::helloWorld(); "htmlcode"><"hello from Debug\n"; } } namespace main; use com\getinstance\util\Debug; // 直接使用到类 Debug::helloWorld(); "color: #0000ff">\表示全局global.php
<"hello from global\n"; } } "hello from ".__NAMESPACE__."\n"; // __NAMESPACE__当前namespace } } Lister::helloWorld(); // access local \Lister::helloWorld(); // access global "color: #0000ff">命名空间加{}<"hello from Debug\n"; } } } namespace main { \com\getinstance\util\Debug::helloWorld(); } "color: #0000ff">全局命名空间<"hello from global\n"; } } } namespace com\getinstance\util { class Lister { public static function helloWorld() { print "hello from ".__NAMESPACE__."\n"; } } Lister::helloWorld(); // access local \Lister::helloWorld(); // access global } "htmlcode"><"ShopProduct constructor\n"; } } "$classname.php" ); } $product = new ShopProduct( 'The Darkening', 'Harry', 'Hunter', 12.99 ); "color: #0000ff">进一步优化处理位于文件夹business/ShopProduct.php
<"business_ShopProduct constructor\n"; } } "$path.php" ); } $x = new ShopProduct(); $y = new business_ShopProduct(); "_blank" href="https://www.jb51.net/Special/43.htm">php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》希望本文所述对大家PHP程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com白云城资源网 Copyright www.dyhadc.com暂无“PHP面向对象程序设计之命名空间与自动加载类详解”评论...更新日志
2025年05月05日2025年05月05日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]