12

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!

5 Answers 5

23

this variable can be accessed like this

echo $this->test;
Sign up to request clarification or add additional context in comments.

Comments

11

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

Comments

7
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

Comments

6

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

1 Comment

in example2 there, doesn't it need to be echo $this->test." again"; ?
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.