2011-06-11 4 views
0

가능한 중복 :
Reference - What does this symbol mean in PHP?
PHP: Static and non Static functions and Objects
In PHP, whats the difference between :: and -> ?PHP 사용하여 클래스

나는 PHP 예에서 클래스를 사용하는 다양한 방법을 보았다

$myclass->method()

또는

MyClass::method()

의 차이점은 무엇입니까?

+2

http://stackoverflow.com/questions/6313783/what-is-the-notation-in-php-used-for – Pav

+0

도 참조하십시오 : [PHP에서, >?] (http://stackoverflow.com/questions/3173501/in-php-whats-the-difference-between-and) – ldg

답변

2

예제에서 $ myclass는 MyClass 클래스의 인스턴스로 표시되며 인스턴스 메소드를 호출합니다. 인스턴스 메소드는 클래스의 인스턴스에서 호출됩니다.

두 번째 예제에서 메서드는 클래스의 static 메서드 인 것으로 나타납니다. 정적 메서드는 클래스 수준에서 호출되며 인스턴스가 필요하지 않습니다.

1

, 생성자 (__construct()이)라고했다 당신이 $myclass = new MyClass()을했을 있도록 객체에서 메소드를 호출되는 첫 번째 등

두 번째 정적 호출입니다 : 어떤 객체가 인스턴스화되지 않으며, 그것은 할 수 없습니다 $this 참조를 사용하십시오. 비 정적 변수가에있어 객체에 특정 동안 정적 변수는 모든 BTW 여기 저기 동일 질문이 닫혀 있지만

, 정적 여기에 몇 가지 좋은 정보를 찾을 수 있습니다. https://stackoverflow.com/questions/3090994/what-does-the-static-keyword-mean-in-oop

0

$myclass->method()을 사용하려면 먼저 클래스의 인스턴스를 만들어야합니다.

$myclass = new myClass();

번째는 제 인스턴스를 생성하지 않고 moethod 액세스하는 데 사용된다.