PHP static 키워드
PHP/매뉴얼 번역 2008/06/19 18:19클래스 멤버 또는 메소드를 static 으로 선언하면 클래스를 인스턴스화 하지않고도 억세스가 가능하다. static 멤버는 인스턴스화된 클래스오브젝트로부터 억세스할 수 없다. (static 멤버로부터는 가능하다. )
PHP 4 와의 호환성을 유지하기위하여 、억세스권(접근권한 : Visibility) 선언이 없는 경우 해당 멤버또는 메소드는 public 으로 선언되어있다고 판단한다.
static 메소드는 오브젝트의 인스턴스를 생성하지 않고 호출할 수 있다. pseudo 변수 $this 는 static 으로 선언된 메소드의 내부에서 이용할 수 없다.
static 프로퍼티는 、화살표연산자 (the arrow operator ) -> 를 사용하여 오브젝트로부터 억세스할수 없다.
non-static 메소드를 정적으로 호출하면 E_STRICT 레벨의 경고가 발생한다.
PHP 5.3.0 이후에서는 변수를 이용하여 클래스를 참조할 수 있다. 변수 값에 키워드를 (예를 들어 self 또는 parent、 static ) 지정할 수 없다.
static 멤버 예
<?php
class Foo
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
class Bar extends Foo
{
public function fooStatic() {
return parent::$my_static;
}
}
print Foo::$my_static . "\n";
$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n"; // Undefined "Property" my_static
print $foo::$my_static . "\n";
$classname = 'Foo';
print $classname::$my_static . "\n"; // As of PHP 5.3.0
print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";
?> static 메소드 예
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
?>
원문링크: http://www.php.net/manual/en/language.oop5.static.php
목차로
PHP 클래스와 오브젝트 ( Classes and Objects ) PHP 5
'PHP > 매뉴얼 번역' 카테고리의 다른 글
| PHP 오브젝트 이터레이션 (0) | 2008/06/20 |
|---|---|
| PHP 오버로드 (0) | 2008/06/20 |
| PHP 오브젝트 인터페이스 (0) | 2008/06/20 |
| PHP 클래스의 추상화 (0) | 2008/06/19 |
| PHP 클래스 정수 (0) | 2008/06/19 |
| PHP static 키워드 (0) | 2008/06/19 |
| PHP 스코프 연산자(::) (0) | 2008/06/19 |
| PHP 억세스권(접근권한) : Visibility (0) | 2008/06/19 |
| PHP 컨스트럭터와 디스트럭터 (0) | 2008/06/19 |
| PHP 오브젝트의 오토로딩 (0) | 2008/06/19 |
| PHP 클래스의 기초 (0) | 2008/06/19 |