【问题标题】:Variable Scope Within PHP ClassPHP 类中的变量范围
【发布时间】:2011-05-21 08:41:29
【问题描述】:

如何在这个类中设置一个全局变量?我试过这个:

class myClass
{
   $test = "The Test Worked!";
   function example()
   {
      echo $test;
   }
   function example2()
   {
      echo $test." again";
   }
}

由于 500 错误而无法完全加载页面。接下来我尝试了这个:

class myClass
{
   public $test = "The Test Worked!";
   function example()
   {
      echo $test;
   }
   function example2()
   {
      echo $test." again";
   }
}

但是当我打印这两个时,我看到的只是“再次”对不起这么简单的问题!

谢谢!

【问题讨论】:

    标签: php scope


    【解决方案1】:

    这个变量可以这样访问

    echo $this->test;
    

    【讨论】:

      【解决方案2】:

      尝试在变量前面添加$this;您可以将第二个示例更改为

      class myClass {
         public $test = "The Test Worked!";
      
         function example() {
            echo $this->test;
         }
      
         function example2(){
            echo $this->test." again";
         }
      }
      

      【讨论】:

        【解决方案3】:
        class Foo {
        
            public $bar = 'bar';
        
            function baz() {
                $bar;  // refers to local variable inside function, currently undefined
        
                $this->bar;  // refers to property $bar of $this object,
                             // i.e. the value 'bar'
            }
        }
        
        $foo = new Foo();
        $foo->bar;  // refers to property $bar of object $foo, i.e. the value 'bar'
        

        请从这里开始阅读:http://php.net/manual/en/language.oop5.basic.php

        【讨论】:

          【解决方案4】:

          实际上有两种方法可以从类内部或外部访问类中的变量或函数,如果它们请求项目是公共的(或在某些情况下受保护)

          class myClass
          {
             public $test = "The Test Worked!";
             function example()
             {
                echo $this->test;
                // or with the scope resolution operator
                echo myClass::test;
             }
             function example2()
             {
                echo $this->test." again";
                // or with the scope resolution operator
                echo myClass::test." again";
             }
          }
          

          【讨论】:

          • 在example2那里,不需要 echo $this->test." again"; ?
          【解决方案5】:

          如果你想要一个实例变量(仅为该类的实例保留),请使用:

          $this->test
          

          (作为另一个答案建议。)

          如果您想要一个“类”变量,请在其前面加上“静态”关键字,如下所示:

          类变量与实例变量的不同之处在于,从该类创建的所有对象实例都将共享同一个变量。

          (注意访问类变量,使用类名,或者'self'后跟'::')

          class myClass
          {
             public static $test = "The Test Worked!";
             function example()
             {
                echo self::$test;
             }
             function example2()
             {
                echo self::$test." again";
             }
          }
          

          最后,如果您想要一个真正的常量(不可更改),请在前面使用“const”(再次使用“self”加上“::”加上常量的名称来访问它(尽管这次省略了“$”):

          class myClass
          {
             const test = "The Test Worked!";
             function example()
             {
                echo self::test;
             }
             function example2()
             {
                echo self::test." again";
             }
          }
          

          【讨论】:

            猜你喜欢
            • 2013-07-28
            • 1970-01-01
            • 1970-01-01
            • 2012-01-19
            • 2012-09-10
            • 2010-12-19
            • 2021-02-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多