【问题标题】:Dynamic class variables动态类变量
【发布时间】:2008-09-19 12:18:11
【问题描述】:

PHP 有自动生成类变量的方法吗?我认为我以前见过类似的东西,但我不确定。

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

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

$this->data 数组始终是关联数组,但它们的键因类而异。有什么可行的方法可以从$this->firstValue 访问$this->data['firstValue'] 而无需定义链接?

如果是的话,有什么缺点吗?

或者如果$this->data 数组不包含该键,是否有一种静态方法来定义链接?

【问题讨论】:

    标签: php oop


    【解决方案1】:

    请看这里:http://www.php.net/manual/en/language.oop5.overloading.php

    你想要的是“__get”方法。链接上有一个您需要的示例。

    【讨论】:

      【解决方案2】:

      使用 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 构造函数,您可以控制在类上设置变量的方式,同时仍将所有值存储在单个数组中。

      希望这会有所帮助:)

      【讨论】:

        猜你喜欢
        • 2023-01-19
        • 2013-09-19
        • 2011-05-24
        • 2021-12-29
        • 2020-06-18
        • 2013-11-12
        • 1970-01-01
        • 1970-01-01
        • 2011-11-20
        相关资源
        最近更新 更多