3

I am trying to access a variable within a class in a function:

class google_api_v3 {

  public static $api_key = 'this is a string';

  function send_response() {
     // access here $api_key, I tried with $this->api_key, but that works only on private and that variable I need also to access it outside the class that is why I need it public.
  }

}

function outside_class() {
  $object = new google_api_v3;
  // works accessing it with $object::api_key
}
3
  • 1
    outside: google_api_v3::$api_key should also work? and inside use: self::$api_key or static::$api_key Commented Sep 4, 2014 at 9:18
  • @RaphaelMüller Yes, working. Commented Sep 4, 2014 at 9:21
  • @user3467855 Btw, since you seem to think it's about variable visibility, the fact that your variable is public or private has nothing to do with your problem. I don't get how "it works only on private" with $this->api_key, it shouldn't work. Commented Sep 4, 2014 at 9:42

3 Answers 3

10

A generic way to use values / methods (including static ones) inside the class is self::

echo self::$api_key;
Sign up to request clarification or add additional context in comments.

3 Comments

self enforces use of current class only when you extend class and you override static variable it will show strange results. That is why it's much better to use static keyword or just point the class name from which u want to get static variable.
@Robert At the moment where I wrote the answer, all the given / hinted solutions were 'ClassName::field', I just wanted to give another solution. I could have insisted more on the generic word, but your answer is doing the job, great.
yep :) but using self can be tricky sometimes :P for example in Singleton when you use traits static:: is the only solution which will be flexible
7

There are many ways to do it no one mentioned about static keyword

you can do inside class:

static::$api_key

You can also use references and keywords like parent, self or using class name.

There is difference between self and static. When you override static variable in class self:: will point to the class where it was called and static:: does is wiser and will check ovverides. There's example from php.net side written in comments I've modifed it a bit just to show differences.

<?php

abstract class a
{
    static protected $test="class a";

    public function static_test()
    {
        echo static::$test; // Results class b
        echo self::$test; // Results class a
        echo a::$test; // Results class a
        echo b::$test; // Results class b
    }

}

class b extends a
{
    static protected $test="class b";
}

$obj = new b();
$obj->static_test();

Output:

class b
class a
class a
class b

More on:

http://php.net/manual/pl/language.oop5.static.php

Comments

3
class google_api_v3 {

  public static $api_key = 'this is a string';

  function send_response() {
     $key = google_api_v3::$api_key
  }

}

function outside_class() {
  $object = new google_api_v3;
  // works accessing it with $object::api_key
}

1 Comment

If I assign a new value in $api_key inside send_response() function, how can I access the assigned value from the function outside a class?

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.