Doug Molineux
Doug Molineux

Reputation: 12431

Variable Scope Within PHP Class

How can I set a global variable in this class? I have tried this:

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

Which failed to load the page completely citing a 500 error. Next I tried this one:

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

But when I printed both of these, all I see is " again" Sorry for such a simple question!

Thanks!

Upvotes: 12

Views: 19872

Answers (5)

Camro
Camro

Reputation: 39

Try adding $this to the front of your variables; you could change the second example to

class myClass {
   public $test = "The Test Worked!";

   function example() {
      echo $this->test;
   }

   function example2(){
      echo $this->test." again";
   }
}

Upvotes: 2

Patrick
Patrick

Reputation: 3172

there are actually two ways to access a variable or function in a class from either within the class or outside it, if they request item is public (or in some cases protected)

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";
   }
}

Upvotes: 6

Matthew Mucklo
Matthew Mucklo

Reputation: 219

If you want an instance variable (preserved only for that instance of the class), use:

$this->test

(as another answer suggested.)

If you want a "class" variable, prefix it with the "static" keyword like this:

The class variable is different than the instance variable in that all object instances created from the class will share the same variable.

(Note to access class variables, use the Class Name, or 'self' followed by '::')

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

Finally if you want a true constant (unchangeable), use 'const' in front (again access it with 'self' plus '::' plus the name of the constant (although this time omit the '$'):

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

Upvotes: 11

deceze
deceze

Reputation: 522081

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'

Please start reading here: http://php.net/manual/en/language.oop5.basic.php

Upvotes: 7

Ish
Ish

Reputation: 29536

this variable can be accessed like this

echo $this->test;

Upvotes: 23

Related Questions