2014-04-25 32 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

虽然整个解决方案是不好的,你可以有一个私人的静态方法,这是在每个其他的公共静态方法中调用。

这不是一个构造函数,它通常是因为构造函数用于构造和对象,而在静态上下文中没有。

因此,对于每个静态调用后发射一个共同的功能,你可以使用:

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(); 

导致成

called 
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; 

这里的问题是,每个方法都会新建构造函数的实例。

您可以按照建议使用Singleton模式,以便在所有静态方法中共享该类的实例。

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; 

所以实例变量$_x的保存时method2()被调用,但构造不叫第二次,你可以看到价值只有一个contructor called在结果中。

而且,要重复一下自己,这个想法是非常可怕的。

+0

谢谢你的解释。 – Speedwheelftw

+0

@Marinescu爱德华希望它帮助你:) –

0

由于该类只有静态方法,所以不需要__construct。声明为私人以防止用作new Example

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

而且关键是静态方法应该没有状态,这样你就不必做初始化

+0

这不是重点。他希望在调用静态方法时调用构造函数 – fluminis

+0

@fluminis对于静态方法来说,构造函数不是必需的。 – 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

如果您使用' - >' – fluminis

+0

访问方法,则应删除'static'关键字在这种情况下创建静态方法没有意义,对吧? – Speedwheelftw

0

您可以创建类的新实例,并调用静态方法,像这样:

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

为了有一个称为构造函数。 但由于方法是静态的,你可以打电话给他们,而不必在类的一个实例:

Example::method1(); 
1

你可以打电话给你的类作为一个单身:

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; 
    } 
} 

这样称呼它: $ MyClass的= myClass :: getInstance();

+0

为什么他会无缘无故地单身? – Smokez

+0

他想静态使用他的方法,同时仍然调用他的构造函数不是他。 – Peter

+0

是的,我正在读这个问题,并注意到..该死的,这太早! – Smokez