Manuel PHP

ArrayAccess::offsetExists

(No version information available, might be only in CVS)

ArrayAccess::offsetExistsWhether a offset exists

Description

abstract public boolean ArrayAccess::offsetExists ( string $offset )

Whether or not an offset exists.

This method is executed when using isset() or empty() on objects implementing ArrayAccess.

Note: When using empty() ArrayAccess::offsetGet() will be called and checked if empty only if ArrayAccess::offsetExists() returns true.

Liste de paramètres

offset

An offset to check for.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.

Note: The return value will be casted to boolean if non-boolean was returned.

Exemples

Exemple #1 ArrayAccess::offsetExists() example

  1. <?php
  2. class obj implements arrayaccess { 
  3.    public function offsetSet($offset, $value) { 
  4.       var_dump(__METHOD__); 
  5.    } 
  6.    public function offsetExists($var) { 
  7.       var_dump(__METHOD__); 
  8.       if ($var == "foobar") { 
  9.          return true; 
  10.       } 
  11.       return false; 
  12.    } 
  13.    public function offsetUnset($var) { 
  14.       var_dump(__METHOD__); 
  15.    } 
  16.    public function offsetGet($var) { 
  17.       var_dump(__METHOD__); 
  18.       return "value"; 
  19.    }  
  20. }  
  21.  
  22. $obj = new obj;  
  23.  
  24. echo "Runs obj::offsetExists()\n";  
  25. var_dump(isset($obj["foobar"]));  
  26.  
  27. echo "\nRuns obj::offsetExists() and obj::offsetGet()\n";  
  28. var_dump(empty($obj["foobar"]));  
  29.  
  30. echo "\nRuns obj::offsetExists(), *not* obj:offsetGet() as there is nothing to get\n";  
  31. var_dump(empty($obj["foobaz"]));  
  32. ?> 

L'exemple ci-dessus va afficher quelque chose de similaire à :

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)


Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique