English 中文(简体)
何时使用购买力平价中的固定方法/地点?
原标题:When to use static methods/fields in PHP?
  • 时间:2011-04-02 07:21:55
  •  标签:
  • php
  • static

我何时应当使用购买力平价中的法定职能/类别/领域? 有什么实际用途?

最佳回答

you should not, it s rarely useful. common usage for statics are factory methods and 单吨::instance()

工厂:

class Point{
  private $x;
  private $y;

  public function __construct($x, $y){
    ...
  }

  static function fromArray($arr){
    return new Point($arr["x"], $arr["y"]);
  } 
}

单吨:

class DB{
  private $inst;

  private function __construct(){
    ...
  }

  static function instance(){
    if ($this->inst)
      return $this->inst;

    return $this->inst = new DB();
  }
}
问题回答

以 Java/PHP等语文使用静态方法。

一个简单的例子是,你想要在你的班级的所有情况下都使用一个变数,任何情况都会改变其价值,你也希望在其他场合得到反映。

   class Foo{
    static $count=0;
    public function incrementCount(){
    self::$count++;
    }

   public function getCount(){
    return self:$count;
   }
  }

如果没有静态,你就可以通过一个物体设定价值,并在其他物体中加以利用。

当我需要简单的职能时,我有时会使用统计局的方法。

in a UserProfile class I have an method that returns an array that is used to pass data back to the class after the array is populated from the html page.

    Class UserProfile{

        Public Static get_empty_array(){

            return array( firstname =>  ,lastname=>  ); //usually much more complex multi-dim arrays

        }

    }

This way the empty array can be used within the class/object and outside as a starting template. I also use Static Methods for functions that would normally be standalone functions but I want to keep them in the class so it is all together but also make them available outside as a static method such as:

    public static convert_data($string){

        //do some data conversion or manipulating here then

        return $ret_value;


    }

    $converted_data = class::convert_data($string);

我确实设有一个通用用户定义功能的图书馆,但我发现,把一些功能列入与它密切相关的类别是多余的。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签