2008-09-19 12 views
6

क्या PHP में स्वत: जेनरेट किए गए वर्ग चर होने का तरीका है? मैं सोचता हूं मैंने पहले ऐसा कुछ देखा है लेकिन मुझे यकीन नहीं है।गतिशील वर्ग चर

public class TestClass { 
    private $data = array(); 

    public function TestClass() { 
     $this->data['firstValue'] = "cheese"; 
    } 
} 

$this->data सरणी हमेशा एक साहचर्य सरणी है, लेकिन वे कुंजी वर्ग के लिए कक्षा से बदल जाते हैं। $this->firstValue से लिंक को परिभाषित किए बिना तक पहुंचने का कोई व्यवहार्य तरीका है?

और यदि ऐसा है, तो क्या इसके लिए कोई डाउनसाइड्स हैं?

या क्या इस तरह से लिंक को परिभाषित करने की एक स्थिर विधि है जो $this->data सरणी में उस कुंजी को शामिल नहीं किया गया है तो विस्फोट नहीं होगा?

उत्तर

7

PHP5 "जादू" __get() विधि का उपयोग करें। यह बहुत तरह काम करेंगे:

public class TestClass { 
    private $data = array(); 

    // Since you're using PHP5, you should be using PHP5 style constructors. 
    public function __construct() { 
     $this->data['firstValue'] = "cheese"; 
    } 

    /** 
    * This is the magic get function. Any class variable you try to access from 
    * outside the class that is not public will go through this method. The variable 
    * name will be passed in to the $param parameter. For this example, all 
    * will be retrieved from the private $data array. If the variable doesn't exist 
    * in the array, then the method will return null. 
    * 
    * @param string $param Class variable name 
    * 
    * @return mixed 
    */ 
    public function __get($param) { 
     if (isset($this->data[$param])) { 
      return $this->data[$param]; 
     } else { 
      return null; 
     } 
    } 

    /** 
    * This is the "magic" isset method. It is very important to implement this 
    * method when using __get to change or retrieve data members from private or 
    * protected members. If it is not implemented, code that checks to see if a 
    * particular variable has been set will fail even though you'll be able to 
    * retrieve a value for that variable. 
    * 
    * @param string $param Variable name to check 
    * 
    * @return boolean 
    */ 
    public function __isset($param) { 
     return isset($this->data[$param]); 
    } 

    /** 
    * This method is required if you want to be able to set variables from outside 
    * your class without providing explicit setter options. Similar to accessing 
    * a variable using $foo = $object->firstValue, this method allows you to set 
    * the value of a variable (any variable in this case, but it can be limited 
    * by modifying this method) by doing something like: 
    * $this->secondValue = 'foo'; 
    * 
    * @param string $param Class variable name to set 
    * @param mixed $value Value to set 
    * 
    * @return null 
    */ 
    public function __set($param, $value) { 
     $this->data[$param] = $value; 
    } 
} 

जादू __get, __set का उपयोग करना, और __isset कंस्ट्रक्टर्स जैसे आप चाहें, जबकि अभी भी एक सारिणी में सभी मान भंडारण चर एक वर्ग पर सेट किया जा करने के लिए आप को नियंत्रित करने के लिए अनुमति देगा।

उम्मीद है कि यह मदद करता है :)

संबंधित मुद्दे