Pages

Wednesday, March 26, 2014

The XHPChild interface

The XHPChild interface 

(HHVM >= 2.5)

Introduction 

XHPChild is the base type of values that can be children of XHP elements.
Most primitive types implement XHPChild: string, int, float, and array.
Note:
Classes that implement XHPChild must do so by implementing the XHPChildClass subinterface.

Interface synopsis 

XHPChild {
}
This interface has no methods, its only purpose is to be the base interface for all XHP classes.

The Continuation class

The Continuation<Tv> class 

(HHVM >= 2.5)

Introduction 

Continuation objects are returned from generators.
Caution
Continuation objects cannot be instantiated via new.

Class synopsis 

Continuation<Tv> implements KeyedIterator<int, Tv> {
/* Methods */
public string getOrigFuncName ( void )
public void send ( mixed $v )
public void raise ( Exception $e )
public int getLabel ( void )
public void update ( int $label , Tv $value )
public int num_args ( void )
public mixed get_arg ( int $index )
/* Inherited methods */
abstract public mixed KeyedIterator<Tv>::key ( void )
}

Table of Contents 

  • Continuation<Tv>::getOrigFuncName — Coming soon
  • Continuation<Tv>::send — ????????
  • Continuation<Tv>::raise — Coming soon
  • Continuation<Tv>::getLabel — Coming soon
  • Continuation<Tv>::update — Coming soon
  • Continuation<Tv>::num_args — Coming soon
  • Continuation<Tv>::get_arg — Coming soon

The ArrayAccess interface

The ArrayAccess<Tk, Tv> interface 

(HHVM >= 2.5)

Introduction 

Interface to provide accessing objects as arrays.

Interface synopsis 

ArrayAccess<Tk, Tv> {
/* Methods */
abstract public boolean offsetExists ( Tk $offset )
abstract public mixed offsetGet ( Tk $key )
abstract public this offsetSet ( Tk $key , Tv $val )
abstract public this offsetUnset ( Tk $key )
}
Example #1 Basic usage
<?hhclass Obj<stringstring> implements ArrayAccess<stringstring> {
  private array 
$container = array();
  public function 
__construct() {
    
$this->container = array(
      
"one"   => 1,
      
"two"   => 2,
      
"three" => 3,
    );
  }
  public function 
offsetSet(string $keystring $value): this {
    if (
is_null($key)) {
      
$this->container[] = $value;
    } else {
      
$this->container[$key] = $value;
    }
  }
  public function 
offsetExists(string $offset): bool {
    return isset(
$this->container[$offset]);
  }
  public function 
offsetUnset(string $offset): this {
    unset(
$this->container[$offset]);
  }
  public function 
offsetGet(string $offset): ?string {
    return isset(
$this->container[$offset]) ? $this->container[$offset] : null;
  }
}
$obj = new Obj();var_dump(isset($obj["two"]));var_dump($obj["two"]);
unset(
$obj["two"]);var_dump(isset($obj["two"]));$obj["two"] = "A value";var_dump($obj["two"]);$obj[] = 'Append 1';$obj[] = 'Append 2';$obj[] = 'Append 3';print_r($obj);
The above example will output something similar to:
bool(true)
int(2)
bool(false)
string(7) "A value"
obj Object
(
    [container:obj:private] => Array
        (
            [one] => 1
            [three] => 3
            [two] => A value
            [0] => Append 1
            [1] => Append 2
            [2] => Append 3
        )

)

Table of Contents 

  • ArrayAccess<Tk, Tv>::offsetExists — Whether a offset exists
  • ArrayAccess<Tk, Tv>::offsetGet — Offset to retrieve
  • ArrayAccess<Tk, Tv>::offsetSet — Offset to set
  • ArrayAccess<Tk, Tv>::offsetUnset — Offset to unset

ArrayAccess<Tk, Tv>::offsetExists

(HHVM >= 2.5)
ArrayAccess<Tk, Tv>::offsetExists  Whether a offset exists

Description 

abstract public boolean ArrayAccess<Tk, Tv>::offsetExists ( Tk $offset )
Whether or not an offset exists.
This method is executed when using isset() or empty() on objects implementing ArrayAccess<Tk, Tv>.
Note:
When using empty() ArrayAccess::offsetGet() will be called and checked if empty only ifArrayAccess::offsetExists() returns TRUE.

Parameters 

key
An offset to check for.

Return Values 

Returns TRUE on success or FALSE on failure.
Note:
The return value will be casted to boolean if non-boolean was returned.

Examples 

Example #1 ArrayAccess::offsetExists() example
<?hhclass Obj<TkTv> implements ArrayAccess<TkTv> {
    public function 
offsetSet(Tk $offsetTv $value): this {
        
var_dump(__METHOD__);
    }
    public function 
offsetExists(Tk $var): bool {
        
var_dump(__METHOD__);
        if (
$var == "foobar") {
            return 
true;
        }
        return 
false;
    }
    public function 
offsetUnset(Tk $var): this {
        
var_dump(__METHOD__);
    }
    public function 
offsetGet(Tk $var): Tv {
        
var_dump(__METHOD__);
        return 
"value";
    }
}
$obj = new Obj();

echo 
"Runs obj::offsetExists()\n";var_dump(isset($obj["foobar"]));

echo 
"\nRuns obj::offsetExists() and obj::offsetGet()\n";var_dump(empty($obj["foobar"]));

echo 
"\nRuns obj::offsetExists(), *not* obj:offsetGet() as there is nothing to get\n";var_dump(empty($obj["foobaz"]));?>
The above example will output something similar to:
Runs obj::offsetExists()
string(17) "obj::offsetExists"
bool(true)

Runs obj::offsetExists() and obj::offsetGet()
string(17) "obj::offsetExists"
string(14) "obj::offsetGet"
bool(false)

Runs obj::offsetExists(), *not* obj:offsetGet() as there is nothing to get
string(17) "obj::offsetExists"
bool(true)

ArrayAccess<Tk, Tv>::offsetGet

(HHVM >= 2.5)
ArrayAccess<Tk, Tv>::offsetGet  Offset to retrieve

Description 

abstract public mixed ArrayAccess<Tk, Tv>::offsetGet ( Tk $key )
Returns the value at specified offset.
This method is executed when checking if offset is empty().

Parameters 

key
The offset to retrieve.

Notes 

Note:
Starting with PHP 5.3.4, the prototype checks were relaxed and it's possible for implementations of this method to return by reference. This makes indirect modifications to the overloaded array dimensions of ArrayAccess<Tk, Tv>objects possible.
A direct modification is one that replaces completely the value of the array dimension, as in $obj[6] = 7. An indirect modification, on the other hand, only changes part of the dimension, or attempts to assign the dimension by reference to another variable, as in $obj[6][7] = 7 or $var =& $obj[6]. Increments with ++ and decrements with -- are also implemented in a way that requires indirect modification.
While direct modification triggers a call to ArrayAccess::offsetSet(), indirect modification triggers a call toArrayAccess::offsetGet(). In that case, the implementation of ArrayAccess::offsetGet() must be able to return by reference, otherwise an E_NOTICE message is raised.

Return Values 

Can return all value types.

See Also 

ArrayAccess<Tk, Tv>::offsetSet

(HHVM >= 2.5)
ArrayAccess<Tk, Tv>::offsetSet  Offset to set

Description 

abstract public this ArrayAccess<Tk, Tv>::offsetSet ( Tk $key , Tv $val )
Assigns a value to the specified offset.

Parameters 

key
The offset to assign the value to.
val
The value to set.

Return Values 

this

Notes 

Note:
The key parameter will be set to NULL if another value is not available, like in the following example.
<?hh
$arrayaccess
[] = "first value";$arrayaccess[] = "second value";print_r($arrayaccess);?>
The above example will output:
Array
(
    [0] => first value
    [1] => second value
)
Note:
This function is not called in assignments by reference and otherwise indirect changes to array dimensions overloaded with ArrayAccess (indirect in the sense they are made not by changing the dimension directly, but by changing a sub-dimension or sub-property or assigning the array dimension by reference to another variable). Instead,ArrayAccess::offsetGet() is called. The operation will only be successful if that method returns by reference, which is only possible since PHP 5.3.4.

ArrayAccess<Tk, Tv>::offsetUnset

(HHVM >= 2.5)
ArrayAccess<Tk, Tv>::offsetUnset  Offset to unset

Description 

abstract public this ArrayAccess<Tk, Tv>::offsetUnset ( Tk $key )
Unsets an offset.
Note:
This method will not be called when type-casting to (unset)

Parameters 

key
The offset to unset.

Return Values 

this

Awaitable<T>::getWaitHandle

(HHVM >= 2.5)
Awaitable<T>::getWaitHandle  Returns the wait handle associated with this Awaitable<T>

Description 

abstractpublicWaitHandle<Tt> Awaitable<T>::getWaitHandle ( void )

Parameters 

This function has no parameters.

Return Values 

An Awaitable<T> (concretely a WaitHandle<T>)