PHP5 OOP新手快速入门例子
来源:互联网 发布:js菜单栏滑动 编辑:程序博客网 时间:2024/04/29 04:16
PHP5 的OOP是个好东西,最近找了些小资料给新手培训和给朋友看,还是老外的东西好,例子短小,有OOP基础的话,一看就
明白了
1)基本的类和实例
明白了
1)基本的类和实例
<?phpclass Animal{var $name;function set_name($text){$this->name = $text;}function get_name(){return $this->name;}}$lion = new Animal;$lion->set_name("Leo");echo "The name of your new lion is ", $lion->name, ".";?>
2) 加上些访问控制符,如private
<?phpclass Animal{private $name;function set_name($text){$this->name = $text;}function get_name(){return $this->name;}}$lion = new Animal;$lion->set_name("Leo");echo "The name of your new lion is ", $lion->name, ".";?>由于用了privae,所以这里是出错了,要用get_name去访问3)构造函数<?phpclass Animal{var $name;function __construct($text){$this->name = $text;}function set_name($text){$this->name = $text;}function get_name(){return $this->name;}}$lion = new Animal("Leo");echo "The name of your new lion is ", $lion->get_name(), ".";?>用 _ _construct()做构造函数( 注意,是两个紧跟着的_)4 使用继承<?phpclass Animal{var $name;function set_name($text){$this->name = $text;}function get_name(){return $this->name;}}class Lion extends Animal{var $name;function roar(){echo $this->name, " is roaring!<BR>";}}echo "Creating your new lion...<BR>";$lion = new Lion;$lion->set_name("Leo");$lion->roar();?>5 Overriding<?phpclass animal{var $name;function set_name($text){$this->name = $text;}function get_name(){return $this->name;}}class Lion extends Animal{var $name;function roar(){echo $this->name, " is roaring!<BR>";}function set_name($text){$this->name = strtoupper($text);}}echo "Creating your new lion...<BR>";$lion = new Lion;$lion->set_name("Leo");$lion->roar();?>输出:LEO is roaring这时子类覆盖了父类的set_name方法了6 访问父类中的被覆盖的方法<?phpclass Animal{var $name;function set_name($text){$this->name = $text;}function get_name(){return $this->name;}}class Lion extends Animal{var $name;function roar(){echo $this->name, " is roaring!<BR>";}function set_name($text){Animal::set_name($text);}}echo "Creating your new lion...<BR>";$lion = new Lion;$lion->set_name("Leo");$lion->roar();?>
- PHP5 OOP新手快速入门例子
- PHP5 OOP新手快速入门例子
- gcc新手快速入门
- Android新手快速入门
- JDBC新手快速入门
- docker新手快速入门
- CSS快速入门例子
- OOP例子
- PHP5 OOP的学习
- vs2008/vs2010新手快速入门必读教程
- 淘宝如何运营 新手快速入门攻略
- Hadoop快速入门:新手学习必备文档
- solr 5.0.0 新手快速入门
- 新手如何快速入门node.js
- 新手如何快速入门深度学习
- 新手快速入门Docker最新版管理工具
- axure8.0快速入门新手教程:电子相册
- axure8.0快速入门新手教程:图库
- 爱是永恒---记学友3.31广州演唱会
- mysql 5中的mysqli新支持的preparestament
- ucos review_1
- mysql5中用事务的模式
- asp.net 中一次上传多个文件
- PHP5 OOP新手快速入门例子
- php5.2 中新的输入检查函数
- [转]REST 学习
- jndi快速上手
- Android数据存储和访问之调用WebService
- tomcat 5.08中虚拟主机设置笔记
- (转)看盘ABC:看盘七大指标
- (转)涨停板研判:两种情况五个含义
- 极限编程学习1