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

search for in the

예약 클래스> <예약어 목록
Last updated: Fri, 24 Jul 2009

view this page in

키워드 목록

이 단어들은 PHP에서 특별한 의미를 갖는다. 이중 일부는 함수처럼 보여지기도하고 상수나 기타 등등 으로 보여질것이다-그러나 그렇지 않다. 실제로: 그들은 언어 구조이다. 다음 단어를 상수, 클래스명, 함수명, 메쏘드명으로 사용할수 없다. 이들을 변수명으로 사용하는 것은 괜찮을수도 있지만, 혼란스럽게 될것이다.

PHP 키워드
abstract (PHP 5부터) and array() as break
case catch (PHP 5부터) cfunction (PHP 4만) class clone (PHP 5부터)
const continue declare default do
else elseif enddeclare endfor endforeach
endif endswitch endwhile extends final (PHP 5부터)
for foreach function global goto (PHP 5.3부터)
if implements (PHP 5부터) interface (PHP 5부터) instanceof (PHP 5부터)
namespace (PHP 5.3부터) new old_function (PHP 4만) or private (PHP 5부터)
protected (PHP 5부터) public (PHP 5부터) static switch throw (PHP 5부터)
try (PHP 5부터) use var while xor
컴파일시 상수
__CLASS__ __DIR__ (PHP 5.3부터) __FILE__ __FUNCTION__ __METHOD__
__NAMESPACE__ (PHP 5.3부터)
언어 상수
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
키워드 목록
Bob
06-Sep-2009 06: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.

예약 클래스> <예약어 목록
Last updated: Fri, 24 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites