PHP对象类变量 [英] PHP object class variable

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

问题描述

我已经在PHP中构建了一个类,我必须声明一个类变量作为一个对象。每次我想声明一个空对象我使用:

I have built a class in PHP and I must declare a class variable as an object. Everytime I want to declare an empty object I use:

$var=new stdClass;

但是如果我用它来声明一个类变量为

But if I use it to declare a class variable as

class foo
{
    var $bar=new stdClass;
}

发生解析错误。有没有办法做到这一点,或者必须声明类变量作为一个对象在构造函数中?

a parse error occurs. Is there a way to do this or must I declare the class variable as an object in the constructor function?

PS:我使用PHP 4。

PS: I'm using PHP 4.

推荐答案

对于类成员,只能以这种方式声明静态值,即 ints code> strings , bools arrays 等。

You can only declare static values this way for class members, i.e. ints, strings, bools, arrays and so on. You can't do anything that involves processing of any kind, like calling functions or creating objects.

你必须在构造函数中执行。

You'll have to do it in the constructor.

相关手册部分: / p>

Relevant manual section:


在PHP 4中,只允许var变量的常量初始化。要使用非常量值初始化变量,您需要一个初始化函数,当从类中构建对象时,该函数会自动调用。这样的函数称为构造函数(见下文)。

In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).

这篇关于PHP对象类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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