包含 标签的文章

解决php类继承关键性问题

这段时间一直被 PHP 的类的属性的方法的继承而困扰,VgotFaster 的开发也因这个问题被卡住了,这两天放下了 VgotFaster 框架的开发在做自己的个人主页的页面,估计 VgotFaster  不会马上出来了,我就随便写一个主页的简单的 PHP 对象来作为主页主站的框架了,结果写着写着也就不那么简单了,完全就是一个框架了,晕呼,我怎么搞的到处都是面向对象和框架啊,不过在视图对控制器的属性和方法访问时也就是类的属性和方法继承时还是碰到这个问题,今晚不知为何心平气也和,终于把这个问题给搞定,让视图可以直接访问控制器的属性和方法了,这次主页的框架写的没有 VgotFaster 那么强的自定义和功能,但是却解决了关键性问题,所以 VgotFaster 得重构了,汗呀,第一个版本还没出来就重来了。继续等待吧。

关于这个框架已经抛弃了对 PHP4 的支持,下面发上主页小框架解决关键性问题的 PHP5 的 __get 和 __call 魔术方法吧。

PHP代码片段

/*  
    关键性控制器属性获取  
    @ create 0:29 2009-7-29  
*/  
public function __get($kName)   
{   
    if(isset($this->V->controller->$kName)) {   
        return $this->V->controller->$kName;   
    } else {   
        return $this->V->$kName;   
    }   
}   
  
/*  
    关键性控制器方法调用  
    @ create 1:05 2009-7-29  
    @ update 1:22 2009-7-29  
*/  
public function __call($kName,$kValues)   
{   
    if($kValues) {   
        $vCounts = count($kValues);   
        $argEval = '';   
        for($i=0;$i<$vCounts;$i++) {   
            $argEval .= ',$kValues['.$i.']';   
        }   
        $argEval = substr($argEval,1);   
        eval('$a = $this->V->controller->$kName('.$argEval.');');   
        return $a;   
    } else {   
        return $this->V->controller->$kName();   
    }   
}


Pader 2009-7-29 2

PHP缓存类更新

缓存真的是个好东西,好玩又有趣.

PHP代码
  1. <?php   
  2. /*  
  3.     常量 CACHE_DIR 定义缓存文件所在目录  
  4. */  
  5.   
  6. define('CACHE_DIR','/data/cache');   
  7.   
  8. class cache {   
  9.     var $cache = array();   
  10.            
  11.     //获取缓存   
  12.     function getCache($cacheName) {   
  13.         if(!array_key_exists($cacheName,$this->cache)) {   
  14.             include $this->cacheFile($cacheName);   
  15.             $this->cache[$cacheName] = $cache[$cacheName];   
  16.         }   
  17.         return $this->cache[$cacheName];   
  18.     }   
  19.   
  20.     //存储缓存   
  21.     function saveCache($cacheName,$cacheVar){   
  22.         $cacheCode = "<?php\r\n//Cache create at ".date('Y-m-d H:i:s')."\r\n";   
  23.         $cacheCode .= '$cache[\''.$cacheName.'\'] = ';   
  24.         $cacheCode .= $this->createVarCode($cacheVar);   
  25.         $cacheCode .= ";\r\n";   
  26.         return $this->writeFile($this->cacheFile($cacheName),$cacheCode) ? true : false;   
  27.     }   
  28.   
  29.     //创建缓存变量原生态代码   
  30.     function createVarCode($myVar,$level=0) {   
  31.         $tabEnd = '';   
  32.         for($i=0;$i<$level;$i++) {   
  33.             $tabEnd .= "\t";   
  34.         }   
  35.         $tab = $tabEnd."\t";   
  36.         if(is_array($myVar)) {   
  37.             $varCode = "array(\r\n";   
  38.             foreach($myVar as $key => $val) {   
  39.                 $key = is_numeric($key) ? $key : '\''.$key.'\'';   
  40.                 if(is_array($val)) {   
  41.                     $varCode .= $tab.$key.' => '.$this->createVarCode($val,$level + 1);   
  42.                 } else {   
  43.                     $varCode .= $tab.$key.' => \''.$this->addslashes($val).'\'';   
  44.                 }   
  45.                 $varCode .= ",\r\n";   
  46.             }   
  47.             if($myVar) {   
  48.                 $varCode = substr_replace($varCode,'',-3,1)."$tabEnd)";   
  49.             }   
  50.         } else {   
  51.             $varCode = '\''.$this->addslashes($myVar).'\'';   
  52.         }   
  53.         return $varCode;   
  54.     }   
  55.        
  56.     function addslashes($text) {   
  57.         $text = addcslashes($text,'\\');  
  58.         $text = addcslashes($text,'\'');   
  59.         return $text;   
  60.     }   
  61.   
  62.     //返回缓存文件地址   
  63.     function cacheFile($cacheName) {   
  64.         $cacheFile = CACHE_DIR.'/'.$cacheName.'.php';   
  65.         return $cacheFile;   
  66.     }   
  67.   
  68.     //判断缓存文件是否存在   
  69.     function cacheExists($cacheName) {   
  70.         $state = file_exists($this->cacheFile($cacheName));   
  71.         return $state;   
  72.     }   
  73.        
  74.     function deleteCache($cacheName) {   
  75.         return @unlink($this->cacheFile($cacheName)) ? true : false;   
  76.     }   
  77.   
  78.     function printCache() {   
  79.         print_r($this->cache);   
  80.     }   
  81.   
  82.     //写文件   
  83.     function writeFile($fileName,$content,$wmode='w'){   
  84.         $s = false;   
  85.         if($fp = @fopen($fileName,$wmode)) {   
  86.             if(flock($fp,LOCK_EX)) {   
  87.                 $s = @fwrite($fp,$content) ? true : false;    
  88.                 flock($fp,LOCK_UN);   
  89.                 fclose($fp);   
  90.             }   
  91.         }   
  92.         return $s;   
  93.     }   
  94. }   

加上这个函数

PHP代码
  1. //从缓存获取并处理   
  2. function GetCache($cacheName,$func='') {   
  3.     global $cache;   
  4.     !defined('CACHE_FUNC') && require_once API('cache_function');   
  5.     if($cache->cacheExists($cacheName)) {   
  6.         $thisCache = $cache->getCache($cacheName);   
  7.     } elseif($func) {   
  8.         eval($func.'();');   
  9.         exit('缓存 <b>'.$cacheName.'</b> 未能找到,系统已经更新,请刷新页面!');   
  10.     }   
  11.     return $thisCache;   
  12. }  

这样子调用

PHP代码
  1. $settings = GetCache('settings','cache_reload_settings');  //从缓存获取设置  

我们先定义了一个函灵敏 cache_reload_settings() ,这样 GetCache  函数会先从缓存获取,没有的话就会调用 cache_reload_settings() 函数更新缓存,其实PHP的 var_export 函数让我很郁闷.

Look:

PHP代码
  1. /*  
  2.     缓存更新函数模块  
  3.     14:25 2009-4-20  
  4. */  
  5. define('CACHE_FUNC',true);   
  6.   
  7. if(!defined('CACHE_DIR')) {   
  8.     require_once API('cache');   
  9. }   
  10.   
  11. if(!$cache) {   
  12.     $cache = new cache;   
  13. }   
  14.   
  15. function cache_reload_settings() {   
  16.     global $db,$cache;   
  17.     $settings = array();   
  18.     $result = $db->query("SELECT * FROM hl_settings");   
  19.     while($row = $db->fetchArray($result)) {   
  20.         $settings[$row['n']] = $row['v'];   
  21.     }   
  22.     $cache->saveCache('settings',$settings);   
  23. }  
Pader 2009-4-29 0

今日写完的缓存类

 尚未完善,大家共同研究

<?php         
/*        
  缓存类 of www.vgot.cn by pader        
  09-1-30 13:34        
*/          
      
define('CACHE_DIR',dirname(__FILE__).'/cache');      
  
class cache {   
    var $cache = array();   
       
    //获取缓存   
    function getCache($cacheName) {   
        include $this->cacheFile($cacheName);   
        $this->cache[$cacheName] = $cache[$cacheName];   
        return $this->cache[$cacheName];   
    }   
  
    //存储缓存   
    function saveCache($cacheName,$cacheVar){   
        $cacheCode = "<?php\r\n//Cache create at ".date('Y-m-d H:i:s')."\r\n";   
        $cacheCode .= '$cache[\''.$cacheName.'\'] = ';   
        $cacheCode .= $this->createVarCode($cacheVar);   
        $cacheCode .= ";\r\n";   
        return $this->writeFile($this->cacheFile($cacheName),$cacheCode) ? true : false;   
    }   
  
    //创建缓存变量原生态代码   
    function createVarCode($myVar,$level=0) {   
        $tabEnd = '';   
        for($i=0;$i<$level;$i++) {   
            $tabEnd .= "\t";   
        }   
        $tab = $tabEnd."\t";   
        if(is_array($myVar)) {   
            $varCode = "array(\r\n";   
            foreach($myVar as $key => $val) {   
                $key = is_numeric($key) ? $key : '\''.$key.'\'';   
                if(is_array($val)) {   
                    $varCode .= $tab.$key.' => '.$this->createVarCode($val,$level + 1);   
                } else {   
                    $varCode .= $tab.$key.' => \''.$this->addslashes($val).'\'';   
                }   
                $varCode .= ",\r\n";   
            }   
            $varCode = substr_replace($varCode,'',-3,1)."$tabEnd)";   
        } else {   
            $varCode = '\''.$this->addslashes($myVar).'\'';   
        }   
        return $varCode;   
    }   
       
    function addslashes($text) {   
        $text = addcslashes($text,'\\');  
        $text = addcslashes($text,'\'');   
        return $text;   
    }   
  
    //返回缓存文件地址   
    function cacheFile($cacheName) {   
        $cacheFile = CACHE_DIR.'/'.$cacheName.'.php';   
        return $cacheFile;   
    }   
  
    function deleteCache($cacheName) {   
        return @unlink($this->cacheFile($cacheName)) ? true : false;   
    }   
  
    function printCache() {   
        print_r($this->cache);   
    }   
  
    //写文件   
    function writeFile($fileName,$content,$wmode='w'){   
        $s = false;   
        if($fp = @fopen($fileName,$wmode)) {   
            if(flock($fp,LOCK_EX)) {   
                $s = @fwrite($fp,$content) ? true : false;    
                flock($fp,LOCK_UN);   
                fclose($fp);   
            }   
        }   
        return $s;   
    }   
}


Pader 2009-1-30 2