使用 PHP 对象访问静态属性 [英] Access Static properties using PHP object

查看:37
本文介绍了使用 PHP 对象访问静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是参考获取实例的静态属性,我是新手,有以下代码:

This is with reference to Get a static property of an instance, I am a newbie and have the following code :

class Foo
{
   public static $my_static = 1;
}

class Bar extends Foo
{

}

$foo = new Foo();
$boo = new Bar();

echo Foo::$my_static;  // ok
echo Bar::$my_static;  // ok
echo $foo::$my_static; // ok
echo $boo::$my_static; // ok

静态变量/属性在 C++ 中仅作为 ClassName::static_property 访问,但在 PHP 中并非如此……但 PHP 书籍大多提到 className::static_property 模式,而不是 object::static_property 结构.对此需要更多了解..

Static variables/properties are accessed only as ClassName::static_property as in C++, but it is not the case in PHP... but PHP books mostly mention the className::static_property pattern, not the object::static_property construct. Need more light on this..

推荐答案

可以通过多种方式访问​​静态属性.

Static properties may be accessed on various ways.

Class::$aStaticProp; //by class name

$classname::$aStaticProp; // As of PHP 5.3.0 by object instance

不能使用箭头操作符->通过对象访问静态属性.

Static properties cannot be accessed through the object using the arrow operator ->.

从 PHP 5.3.0 开始,可以使用变量来引用类.变量的值不能是关键字(例如 self、parent 和 static).

As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).

您可以在手册

这篇关于使用 PHP 对象访问静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆