在类的构造函数中从另一个对象创建对象

发布于 2024-12-10 14:24:55 字数 912 浏览 0 评论 0原文

我想在另一个类中制作另一个类的对象。 所以我做到了:

class National_Number{
    private $num = array();
    public function __construct($first = NULL,$second = NULL,$third = NULL) {
        $this->set($first,0);
        $this->set($second,1);
        $this->set($third,2);
    }
    public function set($num = NULL,$part = 0)
    {
        if ($num != NULL AND $part num[$part] = $num;
            else
                return FALSE;
        }
        else
        {
        return FALSE;
        }
    }
    public function get($part = 0)
    {
        if ($part num[$part];
        else
            return FALSE;
    }
}
class Temp {
    private $ID;
    public function __construct() {
        $ID = new National_Number(127,110,8100);
    }
    public function _get()
    {
        var_dump($ID->get());
    }
}
$temp = new Temp();
$temp->_get();

国家代码可以正常工作,但是 Temp 类无法工作,问题出在哪里?

I wanna make object from another class in other one.
so I done it:

class National_Number{
    private $num = array();
    public function __construct($first = NULL,$second = NULL,$third = NULL) {
        $this->set($first,0);
        $this->set($second,1);
        $this->set($third,2);
    }
    public function set($num = NULL,$part = 0)
    {
        if ($num != NULL AND $part num[$part] = $num;
            else
                return FALSE;
        }
        else
        {
        return FALSE;
        }
    }
    public function get($part = 0)
    {
        if ($part num[$part];
        else
            return FALSE;
    }
}
class Temp {
    private $ID;
    public function __construct() {
        $ID = new National_Number(127,110,8100);
    }
    public function _get()
    {
        var_dump($ID->get());
    }
}
$temp = new Temp();
$temp->_get();

the national code will work correctly,but the Temp class will not work,where is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独闯女儿国 2024-12-17 14:24:55

访问类成员时不要忘记使用“ $this-> ”,更新的代码=>

class National_Number{
            private $num = array();
            public function __construct($first = NULL,$second = NULL,$third = NULL) {
                $this->set($first,0);
                $this->set($second,1);
                $this->set($third,2);
            }
            public function set($nume = NULL,$part = 0)
            {
                if ($nume != NULL AND $part){
                    $this->num[$part] = $nume;
                }
                    else
                        return FALSE;
                }


            public function get($part = 0)
            {
                if (isset($this->num[$part])){
                    return $this->num[$part];
                }
                else
                    return FALSE;
            }
        }
        class Temp {
            private $ID;
            public function __construct() {
                $this->ID = new National_Number(127,110,8100);
            }
            public function _get()
            {
                var_dump($this->ID->get(1));
            }
        }
        $temp = new Temp();
        $temp->_get();
        ?>

Don`t forget to use " $this-> " when accessing class members, updated code=>

class National_Number{
            private $num = array();
            public function __construct($first = NULL,$second = NULL,$third = NULL) {
                $this->set($first,0);
                $this->set($second,1);
                $this->set($third,2);
            }
            public function set($nume = NULL,$part = 0)
            {
                if ($nume != NULL AND $part){
                    $this->num[$part] = $nume;
                }
                    else
                        return FALSE;
                }


            public function get($part = 0)
            {
                if (isset($this->num[$part])){
                    return $this->num[$part];
                }
                else
                    return FALSE;
            }
        }
        class Temp {
            private $ID;
            public function __construct() {
                $this->ID = new National_Number(127,110,8100);
            }
            public function _get()
            {
                var_dump($this->ID->get(1));
            }
        }
        $temp = new Temp();
        $temp->_get();
        ?>
悲念泪 2024-12-17 14:24:55

$ID 是一个局部变量,仅在 __construct() 内有效。如果要引用属性,则必须使用 $this->ID

$this->ID = new National_Number(127,110,8100);

__get() 相同

$ID is a local variable and only valid within __construct(). If you want to refer properties, you must use $this->ID

$this->ID = new National_Number(127,110,8100);

Same for __get()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文