downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Classes prédéfinies> <Mots réservés en PHP
Last updated: Fri, 14 Aug 2009

view this page in

Liste de mots-clés

Ces mots ont un sens spécial en PHP. Certains représentent des objets qui ressemblent à des fonctions, d'autres à des constantes, et ainsi de suite, mais ils n'en sont pas vraiment : ce sont des structures de langage. Vous ne pourrez pas les utiliser comme constante, nom de classes, nom de fonctions ou nom de méthodes. Vous pouvez les utiliser comme nom de variables, mais cela risque d'entraîner des confusions.

Mots réservés en PHP
abstract (depuis PHP 5) and array() as break
case catch (depuis PHP 5) cfunction (PHP 4 uniquement) class clone (depuis PHP 5)
const continue declare default do
else elseif enddeclare endfor endforeach
endif endswitch endwhile extends final (depuis PHP 5)
for foreach function global goto (depuis PHP 5.3)
if implements (depuis PHP 5) interface (depuis PHP 5) instanceof (depuis PHP 5)
namespace (depuis PHP 5.3) new old_function (PHP 4 uniquement) or private (depuis PHP 5)
protected (depuis PHP 5) public (depuis PHP 5) static switch throw (depuis PHP 5)
try (depuis PHP 5) use var while xor
Constantes utilisées lors de la compilation
__CLASS__ __DIR__ (depuis PHP 5.3) __FILE__ __FUNCTION__ __METHOD__
__NAMESPACE__ (depuis PHP 5.3)
Constructeurs de langage
die() echo() empty() exit() eval()
include() include_once() isset() list() require()
require_once() return() print() unset()


add a note add a note User Contributed Notes
Liste de mots-clés
Bob
06-Sep-2009 09:02
There are some cases when you need to use a reserved keyword or language construct as a class method name. In this instance, there is very little chance of namespace conflicts (as the class itself acts as a namespace). If you try to define the method the old way, you will get an unexpected token error.

There is an unobtrusive, and very useful way to use a reserved keyword for a method name. For example, you want to define two class methods 'list' and 'unset' (these two are language builtins and normally not allowed for method names).

<?php
class MyClass
{
   
// Define MyClass::unset() with a different name, e.g. 'rm'
   
public function rm($arg)
    {
       
/* code... */
   
}
   
// Define MyClass::list() with a different name, e.g. 'ls'
   
public function ls($arg = null)
    {
       
/* code... */
   
}
   
// Now define a __call() method (requires PHP > 5.2.3 to take effect)
   
public function __call($func, $args)
    {
        switch (
$func)
        {
            case
'list':
                return
$this->ls((isset($args[0]))? $args[0]: null);
            break;
            case
'unset':
                return
$this->rm($args[0]);
            break;
            default:
               
trigger_error("Call to undefined method ".__CLASS__."::$func()", E_USER_ERROR);
            die ();
        }
    }
}
?>

The only caveat is that to use the long method names, you need PHP > 5.2.3. However, a nice feature is that if you are using an older version than 5.2.3, all of the __call() stuff is ignored and the class will behave as expected (in other words, it degrades gracefully).

You also need to be aware of the methods' expected arguments. MyClass::ls()'s argument is optional, so the extra isset() check is required. If your methods take more arguments, they will need to be manually dereferenced from the $args array, e.g. <?php return $this->my_func($args[0], $args[1], $args[2]);?> for 3 required arguments.

This is a nice trick, and can let you code better APIs for newer versions of PHP. However, if this script is to be run on older PHP installations, be very sure to use the short method names.

Classes prédéfinies> <Mots réservés en PHP
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites