在类的构造函数中从另一个对象创建对象
我想在另一个类中制作另一个类的对象。 所以我做到了:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
访问类成员时不要忘记使用“ $this-> ”,更新的代码=>
Don`t forget to use " $this-> " when accessing class members, updated code=>
$ID
是一个局部变量,仅在__construct()
内有效。如果要引用属性,则必须使用$this->ID
与
__get()
相同$ID
is a local variable and only valid within__construct()
. If you want to refer properties, you must use$this->ID
Same for
__get()