CakeFest 2024: The Official CakePHP Conference

Elenco delle parole chiave

Queste parole hanno un significato speciale in PHP. Alcune di esse rappresentano oggetti che appaiono come funzioni, altre come costanti, e altro ancora - ma non lo sono, in realtà: sono dei costrutti del linguaggio. Non si può usare alcuna delle seguenti parole come constante, nome di classe, nome di funzione o metodo. L'uso di queste parole per nominare una variabile generalmente è consentito ma potrebbe generare confusione.

A partire da PHP 7.0.0 queste parole chiave sono consentite come nomi di proprietà, constanti, e metodi di classi, interfacce e tratti, ad eccezione di class non possono essere usate come nomi di costanti.

Parole chiave di PHP
__halt_compiler() abstract and array() as
break callable (a partire da PHP 5.4) case catch class
clone const continue declare default
die() do echo else elseif
empty() enddeclare endfor endforeach endif
endswitch endwhile eval() exit() extends
final finally (a partire da PHP 5.5) fn (a partire da PHP 7.4) for foreach
function global goto (a partire da PHP 5.3) if implements
include include_once instanceof insteadof (a partire da PHP 5.4) interface
isset() list() match (a partire da PHP 8.0) namespace (a partire da PHP 5.3) new
or print private protected public
require require_once return static switch
throw trait (a partire da PHP 5.4) try unset() use
var while xor yield (a partire da PHP 5.5) yield from (a partire da PHP 7.0)
Costanti Compile-time
__CLASS__ __DIR__ (a partire da PHP 5.3) __FILE__ __FUNCTION__ __LINE__ __METHOD__
__NAMESPACE__ (a partire da PHP 5.3) __TRAIT__ (a partire da PHP 5.4)
add a note

User Contributed Notes 5 notes

up
27
martindilling at gmail dot com
10 years ago
RegEx to find all the keywords:

\b(
(a(bstract|nd|rray|s))|
(c(a(llable|se|tch)|l(ass|one)|on(st|tinue)))|
(d(e(clare|fault)|ie|o))|
(e(cho|lse(if)?|mpty|nd(declare|for(each)?|if|switch|while)|val|x(it|tends)))|
(f(inal|or(each)?|unction))|
(g(lobal|oto))|
(i(f|mplements|n(clude(_once)?|st(anceof|eadof)|terface)|sset))|
(n(amespace|ew))|
(p(r(i(nt|vate)|otected)|ublic))|
(re(quire(_once)?|turn))|
(s(tatic|witch))|
(t(hrow|r(ait|y)))|
(u(nset|se))|
(__halt_compiler|break|list|(x)?or|var|while)
)\b
up
18
Thomas Hansen
7 years ago
Please note that reserved words are still not allowed to be used as namespace or as part of it:

<?php
namespace MyNameSpace\List;

class
Test
{
}
?>

This will fail with a Parse error: syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING)
up
15
Chris
11 years ago
Here they are as arrays:

<?php
$keywords
= array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');

$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');
?>

Along with get_defined_functions() and get_defined_constants(), this can be useful for checking eval() statements.
up
0
Chocopie
5 days ago
From php8, reserved keywords such as `Interface` or `Trait` can be used as part of the namespace.

<?php
namespace App\Entity\Interface;

interface
FooInterface
{

}

https://wiki.php.net/rfc/namespaced_names_as_token
up
-58
Johnny D.
3 years ago
const FORBIDDEN_TYPES = [
'null',

'bool',
'false',
'true',

'int',
'float',

'string',
];
To Top