php:自定义类型的属性

use*_*090 1 php properties

我正在尝试编写以下代码,虽然它是错误的,但它可能会证明我为什么要这么做.

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

我怎样才能做到这一点?

谢谢

Mik*_*wis 5

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

利用构造函数,每次创建新对象时都会调用该构造函数.

  • 是的,谢谢:)我宁愿明确而不是暗示. (2认同)