Показать сообщение отдельно

  #782  
Старый 03.06.2009, 20:39
eLWAux
Постоянный
Регистрация: 15.06.2008
Сообщений: 941
С нами: 9423746

Репутация: 2399


По умолчанию

класс для сохранения переменных в себе

юзаем:
PHP код:
    $a 'aaaaaa';
    include(
'selfVar.php');
   
// simple , how to use this
    
$sv = new selfVar();
    
$sv->saveVar('a','AVALUE');
    
$sv->saveVar('b','BVALUE');
    
$sv->saveVar('ab','NEWWALUE');
    unset(
$sv);
    
$b 'bbbbb';
    echo 
$a.$b
запускаем скрипт, потом смотрим сорс, и увидем что:
$a = 'AVALUE', $BVALUE = 'BVALUE', $ab = 'NEWWALUE';

PHP код:
    // start of class selfVar
    class selfVar{
        public $v,$f,$ff;
        function __construct($f=null){
            $this->f = ($f==null)?$_SERVER['SCRIPT_FILENAME']:$f;
            if (!file_exists($this->f)) $this->f = null;
        }
        function getVars(){
            if ($this->f==null) return null;
            $this->v = null;
            $this->ff= @implode('',@File($this->f));
            if (preg_match_all('/(\$\w+)\s?=\s?(.+?)\s?;/',$this->ff,$m)) {
                unset($m[0]);
                $this->v = $m;
                return $m;
            } else {
                return null;
            }   
        }
        function saveVar($n,$v,$i=1) {
            if ($this->f==null) return null;
            $m = $this->getVars();
            if (in_array('$'.$n,$m[1])){
                if (preg_match('/(\$'.$n.'\s?=\s?)(.+?)(\s?;)(.*)/',$this->ff,$m)) {
                    $f1 = substr($this->ff,0,strpos($this->ff,$m[1]));
                    $f2 = substr($this->ff,strpos($this->ff,$m[1])+strlen($m[1].$m[2].$m[3]),strlen($this->ff));
                    $ff = $f1.$m[1].'\''.$v.'\''.$m[3].$f2;
                    $this->ff = $ff;
                }
            } else {
                    $f1 = substr($this->ff,0,strpos($this->ff,'<?')+2);
                    $f2 = substr($this->ff,strpos('
<?',$this->ff)+strlen($f1)+2,strlen($this->ff));
                    $this->ff = $f1."\n$".$n.' 
= \''.$v."';\n".$f2;
            }
            
$this->s($this->f,$this->ff);
        }
        function 
s($f,$t){
            
$x fopen($f,'w');
            
fwrite($x,$t);
            
fclose($x);
        }
     }
    
// end of class selfVar
http://uasc.org.ua/2009/06/php-selfvar/
 
Ответить с цитированием