简体   繁体   English

从外部访问类变量

[英]Access to class variable from outside

How can I access to class variable from outside without creating new instance in PHP ? 如何在不使用PHP创建新实例的情况下从外部访问类变量? Something like this: 像这样:

class foo
{
    public $bar;
}

echo foo::$bar;

Is it possible or I must create method that will print or return this value and use it or create new instance ( $a = new foo; echo $a->$bar ) ? 有可能还是我必须创建将打印或返回此值并使用它或创建新实例的方法( $a = new foo; echo $a->$bar )?

EDIT: I don't want to create constant but classic variable that will be changed later. 编辑:我不想创建常量,但经典变量将在以后更改。

IF it makes sense to use a static variable: 如果使用静态变量有意义:

class foo{
    public static $bar = 'example';
}

Which could be accessed like so: 可以这样访问:

echo foo::$bar;

make this variable static to access it with out class object. 使此变量静态以使用类对象进行访问。

If you wanted to change static variable value by method then you need to use static method 如果要通过方法更改静态变量值,则需要使用静态方法

you can try like this: 您可以这样尝试:

class foo
{
  public static $bar ="google";
  public static function changeVal($val){
   self::$bar=$val;
  }
}
 foo::changeVal("changed :)");
 echo foo::$bar;

Output : changed :) 输出:已changed :)

Demo : https://eval.in/107138 演示: https : //eval.in/107138

You also can changed it like this without static method: 您也可以在没有静态方法的情况下进行如下更改:

foo::$bar = "changed";

demo : https://eval.in/107139 演示: https : //eval.in/107139

like this: 像这样:

class foo
{
  public static $bar ="google";
}
echo foo::$bar;

Output: google 输出: google

demo: https://eval.in/107126 演示: https : //eval.in/107126

If the value will never change during runtime, then you probably want a class constant ( http://www.php.net/oop5.constants ) 如果该值在运行时不会改变,则可能需要一个类常量( http://www.php.net/oop5.constants

class foo {
    const bar = 'abc';
}

...otherwise you want a public static variable ( http://www.php.net/manual/en/language.oop5.static.php ) ...否则,您需要一个公共静态变量( http://www.php.net/manual/zh/language.oop5.static.php

 class foo {
    public static $bar = 'abc';
}

...either way, access it like this ...无论哪种方式,都可以像这样访问

echo foo::bar;

You can access the class variable without creating instances only when the variable is markesd as static: 仅当将变量标记为静态时,才可以访问类变量而无需创建实例:

class foo
{
  public static $bar;
}

This is how you use a class variable: 这是使用类变量的方式:

// with instantiation
class foo {
    // initialize on declare
    public $bar = 1;
    // or initialize in __construct()
    public function __construct(){
        $this->bar = 1;
    }
}
$foo = new foo();
var_dump($foo->bar);

// static way
class static_foo {
    public static $bar = 1;
}
var_dump(static_foo::$bar);

And this is how you instantiate a class from a random class name string variable. 这就是从随机类名字符串变量实例化一个类的方法。

$foo = new foo();
$random_class_name = $foo->bar;
try {
    // following line throws if class is not found
    $rc = new \ReflectionClass($random_class_name);
    $obj = $rc->newInstance();
    // can be used with dynamic arguments
    // $obj = $rc->newInstance(...);
    // $obj = $rc->newInstanceArgs(array(...));
} catch(\Exception $Ex){
    $obj = null;
}
if($obj){
    // you have a dynamic object
}

What's your actual question? 您实际的问题是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM