2014-04-25 4 views
0

내가 정적 메소드가있는 클래스가있는 경우, 그 클래스의 생성자를 초기화 할 수있는 가장 좋은 방법은 예를 들어, 무엇을 : 생성자가 시작되지 않은PHP - 시작 생성자는

Class Example { 
    public function __construct(){ 
     /*code here*/ 
    } 
    public static function method1() 
     /*code here*/ 
    } 
    public static function method2() { 
     /*code here*/ 
    } 
} 

Example::method1(); 

, 무엇이다 가장 좋은 방법은?

+0

예를 들어 주시겠습니까? – Speedwheelftw

+0

@ user3540050'Example :: method1()'과 같은 정적 표기법이 더 이상 사용되지 않게되었을 때? 어떤 증거? – hindmost

답변

0

전체 솔루션이 좋지 않지만 서로 정적 정적 메서드로 호출되는 개인 정적 메서드가있을 수 있습니다.

이것은 생성자가 아니며 일반적으로 생성자가 생성 및 객체화에 사용되며 정적 컨텍스트에 객체가 없으므로 정상입니다.

그래서, 각각의 정적 호출 후 일반적인 기능을 발사를 위해, 당신은 사용할 수 있습니다

called 
method2; 

에 결과

class Example { 
    private static function common() { 
     echo 'called'; 
    } 
    public static function method1() { 
     self::common(); 
     echo "</br> method1;"; 
    } 
    public static function method2() { 
     self::common(); 
     echo "</br> method2;"; 
    } 
} 

Example::method2(); 

은 또한 정적 방법으로 개체를 구축 할 수 있습니다

class Example { 
    private function __construct() { 
     echo 'contructor called'; 
    } 
    public static function method1() { 
     $self = new self(); 
     echo "</br> method1;"; 
    } 
    public static function method2() { 
     $self = new self(); 
     echo "</br> method2;"; 
    } 
} 

Example::method1(); 

결과 :

contructor called 
method1; 

여기서 문제는 각 메소드가 생성자의 새 인스턴스를 생성 할 수 있다는 것입니다.

제안 된대로 싱글 톤 패턴을 사용하여 모든 정적 메서드에서 클래스의 인스턴스를 공유 할 수 있습니다.

class Example { 

    private static $_inst = null; 

    private $_x = 0; 

    private function __construct() { 
     echo 'contructor called'; 
    } 

    private static function getInstance() { 
     if(self::$_inst == null) { 
      self::$_inst = new self(); 
     } 
     return self::$_inst; 
    } 

    public static function method1() { 
     self::getInstance(); 
     self::getInstance()->_x = 100; 
     echo "</br> method1;"; 
    } 
    public static function method2() { 
     self::getInstance(); 
     echo self::getInstance()->_x; 
     echo "</br> method2;"; 
    } 
} 

Example::method1(); 
Example::method2(); 

에 발생합니다 어떤 :

contructor called 
method1;100 
method2; 

그래서 method2()이라고합니다,하지만 당신이 볼 수있는 생성자가 두 번째로 호출되지 않을 때 저장되는 인스턴스 변수 $_x의 값이 하나의 contructor called있다 그 결과

그리고 나 자신을 되풀이하기 위해,이 모든 아이디어는 끔찍합니다.

+0

설명해 주셔서 감사합니다. – Speedwheelftw

+0

@MarinescuEdward 그것이 당신을 도왔 으면 좋겠다 :) –

0

클래스에는 정적 메서드 만 있으므로 __construct은 필요하지 않습니다. 개인로 선언은 new Example

private function __construct(){ 
    /*code here*/ 
} 

으로 사용 방지하고 포인트는 상태이 안 정적 방법이다, 그래서 당신은 초기화을 할 필요가 없습니다.

+0

여기서 중요한 것은 아닙니다. 그는 정적 메소드를 호출 할 때 생성자가 호출되기를 원합니다. – fluminis

+0

@fluminis 정적 메소드에는 'constructor'이 필요하지 않습니다. – xdazz

0

new 키워드를 사용하면 생성자가 호출됩니다!

정적 메서드를 호출 할 때 없음

0

다음은 예제 코드입니다.

Class Example { 
    public function __construct(){ 
     /*code here*/ 
    } 
    public static function method1() 
     /*code here*/ 
    } 
    public static function method2() { 
     /*code here*/ 
    } 
} 

$obj = new Example(); // Write this way or as below line 
$obj = new Example; // I think first one is better, to initiate the construct function 
$result = $obj->method1(); 
+0

'-> '를 사용하여 메소드에 접근하면'static' 키워드를 제거해야합니다. – fluminis

+0

이 경우 정적 메소드를 만드는 것이 의미가 없으므로, 맞습니까? – Speedwheelftw

0

당신은 클래스의 새 인스턴스를 만들 그래서 같은 정적 메소드를 호출 할 수 있습니다 :

$example = new Example(); 
$example->method1(); 

라는 생성자를 가질. 좋아

class myClass 
{ 
    private static $_instance; 

    /** 
    * Constructor 
    */ 
    private function __construct() 
    { 
     // Construct 
    } 

    /** 
    * Returns itself 
    * 
    * @return self 
    */ 
    public static function getInstance() 
    { 
     if (!self::$_instance) { 
      self::$_instance = new myClass(); 
     } 

     return self::$_instance; 
    } 
} 

가 전화 : 당신은 싱글로 클래스를 호출 할 수

Example::method1(); 
1

: 방법은 정적이기 때문에 그러나, 당신은 클래스의 인스턴스를하지 않고 그들을 호출 할 수 $ myClass가 = myClass :: getInstance();

+0

그는 이유없이 싱글 톤을 만드는 이유는 무엇입니까? – Smokez

+0

그는 여전히 자신의 생성 함수를 호출하는 동안 자신의 메서드를 정적으로 사용하려고했습니다. – Peter

+0

그래, 다시 질문을 읽고 주목했다 .. 젠장, 너무 일찍! – Smokez