声明变量时开头没有 var 符号?

发布于 2024-11-30 07:09:33 字数 493 浏览 2 评论 0原文

我看到类声明 var _hello_kitty = array(); 为什么他们不使用 $?我试图将其设为 public static $_hello_kitty = array(); ,但如果不添加 $ ,它就无法工作。

现在,当我添加 $ 时,通过 _hello_kitty 引用它的其他函数将停止工作。

编辑:哎呀是我的错,我以某种方式从那里删除了 $,我循环了原始文件及其在那里。但它仍然引用 $this->_tpl_vars[$tpl_var] = &$value 没有 $,我

现在 不能使用 self::_tpl_vars[$tpl_var];我确实使用了 self::$_tpl_vars[$tpl_var];但现在出现错误 array_merge() [function.array-merge]: Argument #1 is not an array i

I have seen class that states var _hello_kitty = array(); why they dont use $? i tried to make it public and static and it does not work without adding $i.e. public static $_hello_kitty = array();

now when i do add $ other functions who reference it by _hello_kitty stop working.

EDIT: OOOPS was my bad i somehow removed $ from there, i looped up original file and its there. but its still referencing like $this->_tpl_vars[$tpl_var] = &$value without $ and i cannot use self::_tpl_vars[$tpl_var];

now i did use self::$_tpl_vars[$tpl_var]; but now error comes up array_merge() [function.array-merge]: Argument #1 is not an array i

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘紸啶 2024-12-07 07:09:33

您正在使用 smarty,看起来您正在使用类变量的 PHP 4 表示法:

var

它的 PHP 5 表示法是:

public

但您不需要更改代码,因为 PHP 是向后兼容的。只需将其保留原样,以提醒您事情会发生变化,并且对于您自己的代码,您不会使用 var

如果您实际上需要更改代码,因为它破坏了(而不是您破坏代码因为您想要更改它),您会很早就注意到。

You're using smarty and it looks like you're using the PHP 4 notation of class variables:

var

The PHP 5 representation of it is:

public

But you don't need to change the code because PHP is backwards compatible. Just leave it as is as a reminder that things change and for your own code you won't make use of var.

In case you will actually need to change the code because it breaks (and not you break the code because you want to change it), you will notice early enough.

剧终人散尽 2024-12-07 07:09:33

这就是 PHP 类属性的语法的工作原理。

您可以使用美元符号定义属性,例如

public $publicProperty;
protected $protectedProperty;
private $privatePropertyKeepOutLulz;

当从类实例(即对象)引用它们时,您可以省略美元符号,例如

$obj->publicProperty;
$this->protectedProperty;
$this->privatePropertyKeepOutLulz;

更新

静态属性使用 static 关键字声明

public static $publicStaticProperty;
private static $privateStaticProperty;

,然后引用他们喜欢这样

// From outside the class (only applies to public properties)
ClassName::$publicStaticProperty;

// From within the class
self::$privateStaticProperty;

// From a descendant class (public or protected only)
parent::$property;

That's how the syntax for PHP class properties works.

You define the property with the dollar-sign, eg

public $publicProperty;
protected $protectedProperty;
private $privatePropertyKeepOutLulz;

When referencing them from a class instance (ie object), you omit the dollar-sign, eg

$obj->publicProperty;
$this->protectedProperty;
$this->privatePropertyKeepOutLulz;

Update

Static properties are declared using the static keyword

public static $publicStaticProperty;
private static $privateStaticProperty;

You then reference them like so

// From outside the class (only applies to public properties)
ClassName::$publicStaticProperty;

// From within the class
self::$privateStaticProperty;

// From a descendant class (public or protected only)
parent::$property;
空城之時有危險 2024-12-07 07:09:33

在 PHP 中,变量以美元符号为前缀,这是编程语言的 sigil 。这是计算中的一个重要概念,也是 PHP 识别变量的唯一方法。

来自wikipedia(永远不会说谎):

在很大程度上受 Perl 启发的 PHP 语言中,“$”位于前面
任何变量名。不以此为前缀的名称被视为常量
或函数。

就是这样。

In PHP, variables are prefixed with the dollar sign, which is the programming language's sigil. This is an important concept in computing, and it's the only way PHP is able to identify variables.

From wikipedia (which never, ever lies):

In the PHP language, which was largely inspired by Perl, “$” precedes
any variable name. Names not prefixed by this are considered constants
or functions.

and that's it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文