PHPWales 2020 - June 3rd to June 4th

The Volatile class

(PECL pthreads >= 3.0.0)

Einführung

The Volatile class is new to pthreads v3. Its introduction is a consequence of the new immutability semantics of Threaded members of Threaded classes. The Volatile class enables for mutability of its Threaded members, and is also used to store PHP arrays in Threaded contexts.

Klassenbeschreibung

Volatile extends Threaded implements Collectable , Traversable {
/* Geerbte Methoden */
public Threaded::chunk ( int $size , bool $preserve ) : array
public Threaded::count ( void ) : int
public Threaded::extend ( string $class ) : bool
public Threaded::from ( Closure $run [, Closure $construct [, array $args ]] ) : Threaded
public Threaded::getTerminationInfo ( void ) : array
public Threaded::isRunning ( void ) : bool
public Threaded::isTerminated ( void ) : bool
public Threaded::isWaiting ( void ) : bool
public Threaded::lock ( void ) : bool
public Threaded::merge ( mixed $from [, bool $overwrite ] ) : bool
public Threaded::notify ( void ) : bool
public Threaded::notifyOne ( void ) : bool
public Threaded::pop ( void ) : bool
public Threaded::run ( void ) : void
public Threaded::shift ( void ) : mixed
public Threaded::synchronized ( Closure $block [, mixed $... ] ) : mixed
public Threaded::unlock ( void ) : bool
public Threaded::wait ([ int $timeout ] ) : bool
}

Beispiele

Beispiel #1 New immutability semantics of Threaded

<?php

class Task extends Threaded
{
    public function 
__construct()
    {
        
$this->data = new Threaded();

        
// attempt to overwrite a Threaded property of a Threaded class (invalid)
        
$this->data = new StdClass();
    }
}

var_dump((new Task())->data);

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

RuntimeException: Threaded members previously set to Threaded objects are immutable, cannot overwrite data in %s:%d

Beispiel #2 Volatile use-case

<?php

class Task extends Volatile
{
    public function 
__construct()
    {
        
$this->data = new Threaded();

        
// attempt to overwrite a Threaded property of a Volatile class (valid)
        
$this->data = new StdClass();
    }
}

var_dump((new Task())->data);

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

object(stdClass)#3 (0) {
}
add a note add a note

User Contributed Notes 1 note

up
0
synnus at gmail dot com
2 months ago
<?php

// just use extends  volatile for use array
// is verry good

class libvar extends Volatile
{
    private
$_adresse = '127.0.0.1';
    private
$_port = 10000;
   
    public
$socket;
    public
$list_socket = array();
    public
$list_error = array();
   
    public function
__construct(){ }
   
    public function
set_list($val) { $ct = count($this->list_socket); $this->list_socket[ $ct ] = $val; return $ct; }
    public function
set_socket($val) { $this->socket = $val; return $this->socket; }
    public function
set_error($val) { $this->list_error[ count($this->list_error) ] = $val; }
   
    public function
unset_list($val) { unset($this->list_error[ $val ]); }

    public function
get_socketinlist($val) { return $this->list_socket[$val]; }
    public function
get_adresse() { return $this->_adresse; }
    public function
get_port() { return $this->_port; }
    public function
get_socket() { return $this->socket; }
}

?>
To Top