Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z destruktorem
Forum PHP.pl > Forum > PHP > Object-oriented programming
keyzen
Problem jest taki że destruktor nie działa jak powinien, metoda która ma taki sam kod działa.

Wyskakuje błąd:
Warning: file_put_contents(./sessions/033b5e522aec54902e89542f0fbe68203285a9c0) [function.file-put-contents]: failed to open stream: No such file or directory in F:\htdocs\sessions\index.php on line 31
A plik istnieje. Metoda normalnie zapisuje plik.

Destruktor i metoda:
Kod
public function __destruct()
{
    file_put_contents($this->sessionsDir . $this->sessionID, base64_encode(serialize($this->sessionVars)));
    
    return;
}

public function A_TA_METODA_DZIALA()
{
    file_put_contents($this->sessionsDir . $this->sessionID, base64_encode(serialize($this->sessionVars)));
    
    return;
}


i cały kod:
Kod
<?php

class Session
{
    protected $sessionsDir = './sessions/';
    protected $sessionName = 'sid';
    protected $sessionID;
    protected $sessionVars;
    
    public function __construct($sessionID = null)
    {
        if ($sessionID === null)
        {
            $sessionID = $this->checkCookie();
            
            if ($sessionID === null)
            {
                $sessionID = $this->createNewSession();
            }
        }
        
        $this->sessionID = $sessionID;
        
        $this->assignSessionVars();
        
        return;
    }
    
    public function __destruct()
    {
        file_put_contents($this->sessionsDir . $this->sessionID, base64_encode(serialize($this->sessionVars)));
        
        return;
    }
    
    public function A_TA_METODA_DZIALA()
    {
        file_put_contents($this->sessionsDir . $this->sessionID, base64_encode(serialize($this->sessionVars)));
        
        return;
    }
    
    public function __get($name)
    {
        return $this->sessionVars[$name];
    }
    
    public function __set($name, $value)
    {
        $this->sessionVars[$name] = $value;
        
        return;
    }
    
    protected function checkCookie()
    {
        if (isset($_COOKIE[$this->sessionName]))
        {
            if (preg_match('/^[a-z0-9]+$/i', $_COOKIE[$this->sessionName]) && file_exists($this->sessionsDir . $_COOKIE[$this->sessionName]))
            {
                return $_COOKIE[$this->sessionName];
            }
        }
        
        return null;
    }
    
    protected function createNewSession()
    {
        do
        {
            $sessionID = sha1((string) microtime(1));
        }
        while (file_exists($this->sessionsDir . $sessionID));
        
        file_put_contents($this->sessionsDir . $sessionID, null);
        
        setcookie($this->sessionName, $sessionID);
        
        return $sessionID;
    }
    
    protected function assignSessionVars()
    {
        $this->sessionVars = unserialize(base64_decode(file_get_contents($this->sessionsDir . $this->sessionID)));
        
        return;
    }
    
    public function getSessionName()
    {
        return $this->sessionName;
    }
    
    public function getSessionID()
    {
        return $this->sessionID;
    }
}

$u = new Session();

if ($u->counter === null)
{
    $u->counter = 0;
}

echo ++$u->counter;

$u->A_TA_METODA_DZIALA();

//Ale destruktor juz nie :(
?>
erix
Cytat
be careful while trying to access files with __destruct() because the base directory (getcwd()) will be the root of your server and not the path of your script, so add before all your path called in __destruct() :
EITHER dirname($_SERVER["SCRIPT_FILENAME"])."my/path/"
OR dirname(__FILE__)."my/path/"
(be careful with includes, it will give the path of the file processed and not the main file)


2 minuty przeglądania manuala...
morpheouss
Cytat(erix @ 10.06.2009, 20:24:15 ) *
2 minuty przeglądania manuala...


Nie wiedziałeś o tym mając ZCE?
erix
Nie trzeba mieć nie wiadomo ilu fakultetów, żeby czasem zerknąć do dokumentacji: __destruct" title="Zobacz w manualu PHP" target="_manual. Myślenie nie boli.

Poza tym - masz mi coś do powiedzenia - zapraszam do PW, nie nabijaj postów.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.