php:自定义类型的属性

时间:2011-04-05 23:48:54

标签: php properties

我正在尝试编写以下代码,虽然这是错误的,但它可能会说明我尝试做的最佳原因。

class myClass
{
    private $name = "";
    private $startAddress = new myAddress();  // this is the issue
    private $endAddress = new myAddress();    // this is the issue
}

如何正确完成这项工作?

谢谢

1 个答案:

答案 0 :(得分:5)

 <?php
class myClass {
   private $endAddress;
   public function __construct() {
       $this->endAddress = new myAddress();
   }
 }

利用每次创建新对象时调用的constructor

相关问题