【问题标题】:How do I make variable classwide accessable (Object oriented Programming PHP)如何使变量类范围可访问(面向对象的 PHP 编程)
【发布时间】:2014-05-22 13:15:41
【问题描述】:

我有一个名为testclass 的类和其中的几个函数。他们都需要访问一个预定义变量$need_to_have。我想在类中定义它,但是如何定义呢?

例子:

class testclass {

  // This won't work:
  private static $need_to_have = get_some_info();

  public static testfunction1() {
    // Do Something with $need_to_have
  }

  public testfunction2 () {
    // Do something else with $need_to_have
  }

}

忘了说:我想把它私有化,而且我只在静态函数上调用变量,所以我不能使用构造函数。

【问题讨论】:

  • 将其声明为公开的......但要注意它很容易从您的对象外部发生意外更改。我最好创建一个公共 getter 方法。
  • 不,我定义它的地方用于类常量,并且只允许使用像 true、"String" 和 5 这样的简单值。不允许在其中调用函数甚至其他变量。
  • @Brainiac 那么你对仅仅设置访问级别有什么担心
  • @RoyalBg 你所说的“制定访问级别”是什么意思?
  • testclass::getNeedToHave() 你有 public static getNeedToHave() getter

标签: php oop object


【解决方案1】:

你不能这样做,因为你不能用非常量表达式初始化类属性。

你可以做的是在你和属性之间放置一个访问器方法,例如:

class testclass {

  private static $need_to_have;
  private static $need_to_have_initialized;

  public static testfunction1()
  {
    // Do Something with getNeedToHave()
  }

  private static function getNeedToHave()
  {
      if (!self::$need_to_have_initialized) {
          self::$need_to_have = get_some_info();
          self::$need_to_have_initialized = true;
      }

      return self::$need_to_have;
  }
}

如果有一个常量值get_some_info 保证永远返回,你可以用它来初始化$need_to_have;这将允许您摆脱辅助属性$need_to_have_initialized:

// example: get_some_info() never returns false
private static $need_to_have = false;

private static function getNeedToHave()
{
    if (self::$need_to_have === false) {
        self::$need_to_have = get_some_info();
    }

    return self::$need_to_have;
}

另一个可能的修改(改进?)是在getNeedToHave 中创建属性(和“初始化”标志,如果适用)局部静态变量;这样,它们甚至不会从类本身的任何地方可见:

private static function getNeedToHave()
{
    static $need_to_have = false;

    if ($need_to_have === false) {
        $need_to_have = get_some_info();
    }

    return $need_to_have;
}

【讨论】:

    【解决方案2】:

    这几乎有很多方法可以做到。

    这里最流行的方法是使用所谓的 getter(访问器)方法,该方法将返回有关属性的当前信息,并且它仍然可以是私有的。

    class TestClass {
    
        private static $myVar = 5;
    
        public static getMyVar() {
            return self::$myVar;
        }
    }
    

    因此,通过适当的自动加载器,您将能够在您的所有应用程序中调用

    TestClass::getMyVar();
    

    而且,正如您在 cmets 中所说,没有人可以从课堂外更改它。

    但是,这可能被认为是不好的做法,不仅对于静态方式,而且您不应该引用一个没有任何耦合的类,而只是为了检索信息。

    这里最好的方法是实现注册模式,你可以将信息注册到全局空间。

    创建一个实现注册表设计模式的分离类,并可能设置一些约束,所以一旦设置了你的myVar,没有人可以重置它,以便能够使用以下语法

    Registry::get('myVar');
    

    当然,这将使您有机会在将该属性注册到全局空间之前在该属性中添加更多逻辑,因为定义属性可能会使您在某些定义方面遇到麻烦。

    【讨论】:

      【解决方案3】:

      使其成为类的属性,设置一次,随处使用:

      class testclass 
      {
      
          public static $need_to_have;
      
          public function __construct()
          {
              //$this->need_to_have=1;
              // Or rather the value returned by 
              // whatever it is you are doing with it
              $this->need_to_have=$this->setNeed();
          }
      
          public function testfunction1() 
          {
              // Do Something with $need_to_have
              echo $this->need_to_have
          }
      
          public function testfunction2 () 
          {
              echo $this->need_to_have
          }
      
          public function setNeed()
          {
              $x=4;
              $y=6;
              $ret=$x*$y;
              return $ret;
          }
      
      }
      

      【讨论】:

      • 似乎这个答案已因问题的改变而无效:(
      【解决方案4】:

      没有(面向对象的)方法可以“使variable 类范围可访问”。但这不是必需的。

      您要查找的是对象和类properties 或class constants。

      class TestClass {
      
          const TEST_CLASS_CONSTANT = 'foo';
      
          private static $testClassProperty;
          // or with value: private static $testClassProperty = 'bar';
      
          private $testObjectProperty;
          // or with value: private $testObjectProperty = 'baz';
      
          // This won't work
          // private static $need_to_have = get_some_info();
          // -- since you only can execute functions/methods outside of class structure
          // or within other functions/methods.
      
          public function testMethod() {
              // Writable access to TEST_CLASS_CONSTANT: not possible, since it's a constant.
              // Writable access to $testClassProperty: possible with keywords self, parent, and static.
              self::$testClassProperty = 'newbar';
              // Writable access to $testObjectProperty: possible with keywords this or parent.
              $this->testClassProperty = 'newbaz';
      
              // Readable access to TEST_CLASS_CONSTANT: possible with keywords self, parent, and static.
              echo self::TEST_CLASS_CONSTANT;
              echo PHP_EOL;
              // Readable access to $testClassProperty: possible with keywords self, parent, and static.
              echo self::$testClassProperty;
              echo PHP_EOL;
              // Readable access to $testObjectProperty: possible with keywords this or parent.
              echo $this->testClassProperty;
          }
      
      }
      
      $testObject = new TestClass();
      $testObject->testMethod();
      

      【讨论】:

        猜你喜欢
        • 2013-04-28
        • 1970-01-01
        • 2015-07-20
        • 2021-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多