首页 > 编程技术 > php

PHP面向对象:使用接口与组合模拟多继承(1/2)

发布时间:2016-11-25 15:54

在php中不支持多重继承,如果我们向使用多个类的方法而实现代码重用有什么办法么?那就是组合。在一个类中去将另外一个类设置成属性。

下面的例子,模拟了多重继承。

view sourceprint?01 <? 

02 class user { 

03     private $name = "tom"; 

04     public function getname(){ 

05         return $this->name; 

06     } 

07 } 

08 class teacher{ 

09     private $lengthofservice = 5; // 工龄  

10     public function getlengthofservice(){ 

11         return $this->lengthofservice; 

12     } 

13 } 

14 // 上面的类中的set方法就不写了. 

15 // 如果有个研究生,既是学生也算工龄.  

16 class graduatestudent extends user { 

17     private $teacher ; 

18     public function __construct(){ 

19         $this->teacher = new teacher();       

20     }    

21     public function getlengthofservice(){ 

22         return $this->teacher->getlengthofservice(); 

23     } 

24 } 

25 $graduatestudent = new graduatestudent(); 

26 echo "name is ".$graduatestudent->getname()."<br>"; 

27 echo "lengthofservice is ".$graduatestudent->getlengthofservice(); 

28   

29 ?> 

 代码如下 复制代码
if(!isset($_get[absolutepage])){
        $absolutepage=1;//当前页
       }
       if ($_get[absolutepage]==0){
        $absolutepage=1;
       }else {
        $absolutepage=$_get[absolutepage];
        $absolutepage=intval($absolutepage);
        }
       $pagesize=5;//一页现实的数量
       $a= (($absolutepage - 1) * $pagesize);
       $sql1=$sql."  limit $a,$pagesize ";
     
       $nums=mysql教程_num_rows($query);//总数
       $pagecount = (int)(($nums - 1) / $pagesize) + 1;//总页数
       if ($absolutepage>1 && $nums>1){
           $prevpage=$absolutepage-1;
       }
       if ($absolutepage>=1 && $absolutepage<$pagecount){
        $nextpage=$absolutepage+1;
       }
       if ($absolutepage > $pagecount) {
        $absolutepage = $pagecount;
       }
    $prevpage="<a href='".(strpos($_server['request_uri'],'?') ? "{$_server['request_uri']}&" : '?')."absolutepage=$prevpage'>上一页</a>";
$nextpage="<a href='".(strpos($_server['request_uri'],'?') ? "{$_server['request_uri']}&" : '?')."absolutepage=$nextpage'>下一页</a>";

 

 代码如下 复制代码

$ss = "<a href='1.gif'>d</a>adfxx<a href="dir.html">dir</a>";

print_r(gethref($ss));
   
function gethref($temp){
        preg_match_all('/<a.*?(?: |\t|\r|\n)?href=['"]?(.+?)['"]?(?:(?: |\t|\r|\n)+.*?)?>(.+?)</a.*?>/sim',$temp,$a);
        return $a[1];
    }


//下面所有a连接

 代码如下 复制代码

$htm = preg_replace( "@<a(.*?)</a>@is","$1",$ss);
print_r($htm);


//比较全的提取url连接地址

 

 代码如下 复制代码
 $url="http://www.111cn.net";
 $html=file_get_contents($url,"r");
 preg_match_all ("/(<a)(.*)(href=)([^>]*)(>)([^<]*)(<)([^>]*)(>)/", $html, $matches);
 for ($i=0;$i<count($matches[0]);$i++)
 {
  echo "|||".$matches[2][$i]."||".$matches[3][$i].$matches[4][$i]."||".$matches[6][$i]."||".$matches[8][$i]."<br>";
 }

//提取文章字符串中所有链接地址

 代码如下 复制代码
preg_match_all('/(?<=href=")[wd.:/]*/',$str,$fstr);
 代码如下 复制代码
//方法一
 $body ='<img" width=100% src="image/2009112422220515.gif" alt="楼盘网111cn.net"  border="0" />';
 preg_match_all("/(src|src)=["|'| ]{0,}(image/(.*).(gif|jpg|jpeg|png|bmp))/isu",$body,$img_array);
 print_r($img_array);
 
 //方法二
 preg_match_all("/src="(file:///.*?.png)"/i",$body,$a);
 
 //方法三
 preg_match_all("/<p>.*src="([^^]*?)".*</p>/i",$body,$match);
  print_r($match[1]);
 
 //方法四
 
 echo preg_replace('/<imgs+src="(.*)/(.*)">/i','<img" width=100% src="file/$2">',$body);

 

 代码如下 复制代码
$dm = 'www.111cn.net';
$ip = gethostbyname($dm);
echo gethostbyaddr($ip);
echo $ip;

//两个输出结果是一样的,
/*
关于 gethostbyname语法

string gethostbyname ( string $hostname )

返回由主机名指定的互联网主机的ipv4地址
returns the ipv4 address of the internet host specified by hostname


下面有个例这是最好的方法我已经提出,以解决任何主机名称到ip地址时,它的快速,可靠,已超时的支持!一个无效的地址,例如unicode字符串,返回后4?秒,而不是8调用gethostbyname?!它只能与unix虽然。
*/

 代码如下 复制代码
function getaddrbyhost($host, $timeout = 3) {
   $query = `nslookup -timeout=$timeout -retry=1 $host`;
   if(preg_match('/ address: (.*) /', $query, $matches))
      return trim($matches[1]);
   return $host;
}

/*
gethostbyaddr 是获取internet主机名对应一个特定的ip地址

 代码如下 复制代码
string gethostbyaddr ( string $ip_address )

*/

 代码如下 复制代码

$hostname = gethostbyaddr($_server['remote_addr']);

echo $hostname;

本站原创教程转载www.111cn.net

标签:[!--infotagslink--]

您可能感兴趣的文章: