【十六】php 面向对象
__set()方法:
语法:
function __set($property, $value) {
//$property接收的属性的名字
//$value接收的是属性的值
}
1 class Employee{
2 public $name;
3 private $head;
4 protected $body;
5 function __set($propName,$propValue){
6 echo $propName."<br/>";
7 echo $propValue."<br/>";
8 }
9 }
10 $employee=new Employee();
11 $employee->name="mario"; //公共属性
12 $employee->title="executive chef"; //在Employee类中不存在的属性
13 $employee->head="my head"; //私有属性
14 $employee->body="my body"; //私有属性
__get()方法:
语法:
funciton __set($property) {
//$property接收的是属性的名字
}
1 class Employee{
2 public $publicname;
3 public $city;
4 protected $protectedvalue=15;
5 private $privatevalue;
6 function __get($propName){
7 echo "__get called<br/>";
8 $vars=array("publicname","city","protectedvalue");
9 if (in_array($propName, $vars)) {
10 return $this->$propName."<br/>";
11 }else{
12 return "no such variable<br/>";
13 }
14 }
15 }
16 $employee=new Employee();
17 $employee->publicname="huahua";
18 echo $employee->publicname."<br/>"; //公共属性
19 echo $employee->nothingvalue; //在Employee类中不存在的属性
20 echo $employee->protectedvalue; //私有属性
21 echo $employee->privatevalue; //私有属性
总结:从上图可以看出。
1.如果没有__set()和__get()方法,访问一个类中不存在的属性或没有访问权限的属性时,会报严重错误
2.当实例化一个对象后,调用类中不存在或者没有权限访问(protected、private)的属性的时候,php会默认调用__get()、__set()方法
3.属性为public,也会调用__get()和__set()方法
使用面向对象 写一个个人主页
page.inc.php
1 <?php 2 class page{ 3 public $content; 4 public $title="tla consulting pty ltd"; 5 public $keywords="tla consulting,three latter abbreviation,some of my best friends are search engines"; 6 public $buttons=array("home"=>"home.php", 7 "contact"=>"contact.php", 8 "services"=>"services.php", 9 "site map"=>"map.php" 10 ); 11 public function __set($name,$value){ 12 $this->name=$value; 13 } 14 public function display(){ 15 echo "<html>\n<head>\n"; 16 $this->displaytitle(); 17 $this->displaykeywords(); 18 $this->displaystyles(); 19 echo "</head>\n<body>\n"; 20 $this->displayheader(); 21 $this->displaymenu($this->buttons); 22 echo $this->content; 23 $this->displayfooter(); 24 echo "</body>\n</html>\n"; 25 } 26 public function displaytitle(){ 27 echo "<title>".$this->title."</title>"; 28 } 29 public function displaykeywords(){ 30 echo "<meta name=\"keywords\" content=\"".$this->keywords."\"/>"; 31 } 32 public function displaystyles(){ 33 ?> 34 <style> 35 h1{ 36 color:white;font-size:24pt;text-align:center; 37 font-family:arial,sans-serif 38 } 39 .menu{ 40 color:white;font-size:12pt;text-align:center; 41 font-family:arial,sans-serif;font-weight:bold 42 } 43 td{ 44 background:black 45 } 46 p{ 47 color:black;font-size:12pt;text-align:justify; 48 font-family:arial,sans-serif 49 } 50 p.foot{ 51 color:white;font-size:9pt;text-align:center; 52 font-family:arial,sans-serif;font-weight:bold 53 } 54 a:link,a:visited,a:activep{ 55 color:white 56 } 57 </style> 58 <?php 59 } 60 public function displayheader(){ 61 ?> 62 <table width=100% cellpadding="12" cellspacing="0" border="0"> 63 <tr bgcolor="black"> 64 <td align="left"><img src="logo.jpg"/></td> 65 <td> 66 <h1>huahua's page</h1> 67 </td> 68 <td align="right"><img src="logo.jpg"/></td> 69 </tr> 70 </table> 71 <?php 72 } 73 public function displaymenu($buttons){ 74 echo "<table width=\"200\" bgcolor=\"white\" cellpadding=\"4\"cellspacing=\"4\">\n"; 75 echo "<tr>\n"; 76 $width=200/count($buttons); 77 while (list($name,$url)=each($buttons)) { 78 //注意: !$this->IsURLCurrentPage 79 $this->displaybutton($width,$name,$url,!$this->IsURLCurrentPage($url)); 80 } 81 echo "</tr>\n"; 82 echo "</table>\n"; 83 } 84 public function IsURLCurrentPage($url){ 85 //判断这个url是否指向当前页面. 86 //如果指向当前页面返回true 87 if (strpos($_SERVER['PHP_SELF'],$url)==false) { 88 return false; 89 }else{ 90 return true; 91 } 92 } 93 public function displaybutton($width,$name,$url,$active=true){ 94 if ($active) { 95 echo "<td width=\"".$width."%\"> 96 <a href=\"".$url."\"> 97 <img src=\"s-logo.jpg\" alt=\"".$name."\" border=\"0\"/></a> 98 <a href=\"".$url."\"><span class=\"menu\">".$name."</span></a> 99 </td>"; 100 }else{ 101 echo "<td width=\"".$width."%\"> 102 <img src=\"side-logo.jpg\"><span class=\"menu\">".$name."</span></td>"; 103 } 104 } 105 public function displayfooter(){ 106 ?> 107 <table width="100%" bgcolor="black" cellpadding="12" border="0"> 108 <tr> 109 <td> 110 <p class="foot">©tla consulting pty ltd.</p> 111 <p class="foot">please see our<a href="">legal infotmation page</a></p> 112 </td> 113 </tr> 114 </table> 115 <?php 116 } 117 } 118 ?>
home.php
1 <?php 2 require('page.inc.php'); 3 $homepage=new page(); 4 $homepage->title="huahua girl is best!"; 5 $homepage->content="huahua girl have good good study,day day up!!!"; 6 $homepage->display(); 7 ?>
service.php
1 <?php 2 require('page.inc.php'); 3 class servicespage extends page{ 4 private $roe2buttons=array( 5 "reengineering"=>"reengineering.php", 6 "standards compliance"=>"standards.php", 7 "buzzword conliance"=>"buzzword.php", 8 "mission statements"=>"mission.php" 9 ); 10 //继承了page类 新增了及格菜单 11 public function display(){ 12 echo "<html>\n<head>\n"; 13 $this->displaytitle(); 14 $this->displaykeywords(); 15 $this->displaystyles(); 16 echo "</head>\n<body>\n"; 17 $this->displayheader(); 18 $this->displaymenu($this->buttons); 19 $this->displaymenu($this->roe2buttons); 20 echo $this->content; 21 $this->displayfooter(); 22 echo "</body>\n</html>\n"; 23 } 24 } 25 $service=new servicespage(); 26 $service->content="<p>this is service page</p>"; 27 $service->display(); 28 ?>
界面显示: