【问题标题】:PHP class properties inside properties属性中的 PHP 类属性
【发布时间】:2019-02-02 02:46:44
【问题描述】:

嘿,我一直在阅读一些 PHP 类文档以及如何创建类。但是我还没有看到属性中的任何属性示例。我将举一个我想做的例子:

class Properties {
public property1;
public nestedProperty1;
public nestedProperty2;
}

我想分配这样的属性:

$Property = new Properties();
$Property->property1 = "foo";
$property1->nestedProperty1 = "bar";

所以我可以访问如下数据:

$property1->nestedProperty1;

这可能吗?我需要这个,因为我正在使用动态多维数组。

【问题讨论】:

  • 第一个,你的变量的开头必须有$ - public $property1; 第二,如果你想要$Property->property1->nestedProperty1这样的东西,你可以分配$Property->property1 = new Properties();

标签: php arrays class properties


【解决方案1】:

1) 类!= 数组。

2) 你想要嵌套类。

例如(伪代码):

// Create a class
MyThingClass {
  string ThingProperty1
  int ThingProperty2
}

// Create a MyThingClass instance
MyThing = new MyThingClass(...)

// Use instance in another class
MyOtherThingClass {
  MyThingClass MyThing
  string SomeOtherProperty
}

您不能像尝试那样直接嵌套属性。您也可以将结构用作属性。

【讨论】:

    猜你喜欢
    • 2014-03-05
    • 2012-02-25
    • 2018-08-13
    • 1970-01-01
    • 2010-10-26
    • 2012-06-22
    • 1970-01-01
    • 2013-04-14
    • 2010-10-03
    相关资源
    最近更新 更多