【问题标题】:php: property of custom typephp:自定义类型的属性
【发布时间】:2011-04-05 23:48:53
【问题描述】:

我正在尝试编写以下代码,虽然它是错误的,但它可能会最好地说明我为什么要这样做。

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

我怎样才能正确地做到这一点?

谢谢

【问题讨论】:

    标签: php properties


    【解决方案1】:
     <?php
    class myClass {
       private $endAddress;
       public function __construct() {
           $this->endAddress = new myAddress();
       }
     }
    

    利用constructor,每次创建新对象时都会调用它。

    【讨论】:

    • @alex:如果省略可见性,则假定为public
    • @KingCrunch 我喜欢展示它。否则感觉就像我还在使用 PHP4 :P
    • 非常感谢大家!反应这么快!这是一个很大的帮助。再次感谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    相关资源
    最近更新 更多