메신저 C에서 # 그리고 나는 조금 더 배우려고 ... 이제 정적 ArrayList를 이해하는 데 어려움이 있습니다. PHP에서정적 ArrayList는 값을 메모리에 저장합니까?
난 정의 할 수
Class Singleton{
private static $instance;
private static $arrayDemo = array();
private function __construct(){}
public static function getInstance(){
if(!isset(self::$instance))
self::$instance = new Singleton();
return self::$instance;
}
public static addItem($item){
self::$arrayDemo[] = $item;
}
public static getItems(){
return self::$arrayDemo[];
}
}
Singleton::getInstance();
Singleton::addItem("first");
Singleton::addItem("second");
Singleton::getItems(); // returns {0=>first,1=>second}
을 난 페이지를 재로드하는 경우 나 동일한 결과를 얻었다 (0 => 제 1 => 제)
임 C#의 단일 패턴을 구현하려고 내가 3 번 반환을 갱신하는 경우, 나는이 웹 사이트를 한 번 새로 고치면 내가 PHP와 같은 결과를 얻었다
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
private static ArrayList ArrayDemo = new ArrayList();
private Singleton() { }
public static Singleton Instance
{
get
{
return instance;
}
}
public static void AddItem(string item)
{
ArrayDemo.Add(item);
}
public static ArrayList GetItems()
{
return ArrayDemo;
}
}
//in cshtml:
Singleton.AddItem("first");
Singleton.AddItem("second");
Singleton.GetItems();
...하지만 .. : : 같은 일을 얻을 수 있습니다,하지만 난 값을 반복있어
을0=>first,1=>second,2=>first,3=>second,4=>first,5=>second
왜 이런 일이 발생합니까? 정적 메서드를 사용하면 새로 고침 ArrayList 결과를 지울 수 있습니까? 이 논리를 이해하고 싶습니다.
감사합니다.
그게 문제입니다 : PHP는 * 실제 * 정적 인스턴스를 모른다 :) 참조 http://stackoverflow.com/questions/468642/is-it-possible-to-create-static-classes-in-php- like-in-c/468653 # 468653 and http://stackoverflow.com/questions/432192/singleton-in-php –
@AndreasNiedermair 내가 C#에서 더 많은 것을 배울 때, 나는 PHP 세계에서 5 년을 잃었다 고 느꼈다 : ( – Carasuman