2010-07-23 4 views

답변

1

:: 구문은 사용자가 static 메서드를 호출하고 있음을 의미합니다. ->은 정적이지 않은 반면.

MyClass{ 

    public function myFun(){ 
    } 

    public static function myStaticFun(){ 
    } 

} 

$obj = new MyClass(); 

// Notice how the two methods must be called using different syntax 
$obj->myFun(); 
MyClass::myStaticFun(); 
+0

이것은 사실이 아닙니다. 정적 메소드는'->'로 호출 할 수 있지만 (엄격하지 않아도되지만 엄격한 경고를 통해서조차하지 않는다),'::'를 사용하여 인스턴스 메소드를 호출 할 수있다 :: parentImplementationOfThisMethods()'또는'$ obj :: myFun()'과 같이 사용하면 안됩니다). – Artefacto

2

::은, (일반적으로)정적 메소드, 변수 또는 상수에 액세스 scope resolution 사용된다. 즉

는, 일반적인 구문은 ...

ClassName::MemberName 

... 당신이 $variable::MemberName을 볼 수있는 드문 경우

$Instance->MemberName 

이다, 어떤 사실에 무슨 것은이 그 $variable의 내용은으로 으로 처리되므로 $var='Foo'; $var::BarFoo::Bar과 동일합니다.

http://www.php.net/manual/en/language.oop5.basic.php

http://www.php.net/manual/language.oop5.paamayim-nekudotayim.php

0

:: 기능은 정적 기능이며, 실제로 표기 : 오히려 같은 $ 인스턴스 :() 함수보다)

클래스 :: 기능 (제안해라.

또한 부모의 메소드를 참조하는 서브 클래스에

클래스 :: 기능()

를 사용할 수 있습니다.

1

예 :

class FooBar { 
    public function sayHi() { echo 'Hi!'; } 
    public /* --> */ static /* <-- */ function sayHallo() { echo 'Hallo!'; } 
} 

// object call (needs an instance, $foobar here) 
$foobar = new FooBar; 
$foobar->sayHi(); 

// static class call, no instance required 
FooBar::sayHallo(); // notice I use the plain classname here, not $foobar! 

// As of PHP 5.3 you can write: 
$nameOfClass = 'FooBar'; // now I store the classname in a variable 
$nameOfClass::sayHallo(); // and call it statically 

$foobar::sayHallo(); // This will not work, because $foobar is an class *instance*, not a class *name* 
0

:: 정상적으로 static 방법 또는 Class Constants 호출에 사용된다. (즉, new으로 개체를 인스턴스화 할 필요가 없습니다.) 메서드를 사용합니다. ->은 이미 객체를 인스턴스화 한 것입니다. 예를 들어

: 참고로

Validation::CompareValues($val1, $val2); 

$validation = new Validation; 
$validation->CompareValues($val1, $val2); 

, 당신이 그것을 정의 할 때 사용되는 static 키워드가 있어야합니다 (또는 ::와) 같은 정적 사용하려고 어떤 방법. 이 글에서 링크 된 다양한 PHP.net 문서 페이지를 읽으십시오.

0

::클래스의 상수, 속성 또는 메소드에 액세스 할 수 있습니다.; 변수와 메소드를 static으로 선언해야합니다. 그렇지 않으면 인스턴스와 클래스에 속하지 않습니다.

그리고 ->을 사용하면 클래스 인스턴스의 속성 또는 메소드에 액세스 할 수 있습니다.

관련 문제