PHP 对象类变量
我在 PHP 中构建了一个类,并且必须将类变量声明为对象。每次我想声明一个空对象时,我都会使用:
$var=new stdClass;
但是如果我用它来声明一个类变量,则会
class foo
{
var $bar=new stdClass;
}
发生解析错误。有没有办法做到这一点,或者我必须在构造函数中将类变量声明为对象?
PS:我使用的是 PHP 4。
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: I'm using PHP 4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只能以这种方式为类成员声明静态值,即
ints
、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.
Relevant manual section:
类和对象 (PHP 4)。每次都读得好!
Classes and Objects (PHP 4). A good read everytime!
你不应该在这里创建你的对象。
你应该更好地编写setter和getter
顺便说一句,你应该使用PHP5来与OOP一起工作,这更灵活......
You should not create your object here.
You should better write setter and getter
By the way, you should use PHP5 to work with OOP, which is more flexible...