<?php
/*
?* WEB开发笔记 www.chhua.com 每日练习 PHP面向对象编程——PHP对象引用实例代码
?*/

/*代码演示
?* */
class HelloWorld {//被引用对象
?public $world;
?function __construct($world){
??$this->world=$world;
?}
?
?function getHtml(){
??????? return “Hello “.$this->world.”!”;
?}
}

class Hello {//引用对象
?public $hi;
?public $web;
?
?function __construct(HelloWorld $hi,$web){
??$this->hi=$hi;
??$this->web=$web;
?}
?
?function getHtml(){
??return $this->hi->getHtml();//调用被引用对象的方法
?}
}

$hello=new HelloWorld(“www.chhua.com“);
$hi=new Hello($hello,”web开发”);
echo $hi->getHtml();
/*道理很简单,关键是在运用*/
?>

自由转载,转载请注明: 转载自WEB开发笔记 www.chhua.com

本文链接地址: PHP面向对象编程——PHP对象引用实例代码 http://www.chhua.com/web-note928

相关笔记

更多