php ,我自定义了两个类,A,B,我想将A作为B的一个成员,但是怎么定义,怎么初始化,
在B的成员函数中,怎么调用呢?我的写法<pre>classA{publicfunctionShow(){echo"AAAAAAAA";}}classB{public$m_...
在B的成员函数中,怎么调用呢?我的写法
<pre>
class A
{
public function Show(){echo "AAAAAAAA";}
}
class B
{
public $m_aa;
public function init($obj)
{
$m_aa = $obj;
}
public function test()
{
$m_aa->Show();
}
}
调用B: $bbb = new B():
$bbb->init(new A());
$bbb->test();
</pre>
总是说,我调了个non object对象,搞不明白什么情况,希望大家能指点一二,谢谢了,没有财富值了。。。。。。。。
<?php
class A
{
public function show()
{
echo "<script language='javascript' type='text/javascript'>";
echo "alert(\"asdasdasasdasdasd\")";
echo "</script>";
}
}
class B
{
public $m_a;
public function init($obj)
{
$m_a = $obj;
}
public function test()
{
$this->m_a->show();
}
}
$B = new B();
$B->init(new A());
$B->test();
?>
PHP Fatal error: Call to a member function show() on a non-object in F:\Studio\bestbear\test.php on line 21 展开
<pre>
class A
{
public function Show(){echo "AAAAAAAA";}
}
class B
{
public $m_aa;
public function init($obj)
{
$m_aa = $obj;
}
public function test()
{
$m_aa->Show();
}
}
调用B: $bbb = new B():
$bbb->init(new A());
$bbb->test();
</pre>
总是说,我调了个non object对象,搞不明白什么情况,希望大家能指点一二,谢谢了,没有财富值了。。。。。。。。
<?php
class A
{
public function show()
{
echo "<script language='javascript' type='text/javascript'>";
echo "alert(\"asdasdasasdasdasd\")";
echo "</script>";
}
}
class B
{
public $m_a;
public function init($obj)
{
$m_a = $obj;
}
public function test()
{
$this->m_a->show();
}
}
$B = new B();
$B->init(new A());
$B->test();
?>
PHP Fatal error: Call to a member function show() on a non-object in F:\Studio\bestbear\test.php on line 21 展开
展开全部
public function init($obj)
{
$m_a = $obj;
}
改为:
public function init($obj)
{
$this->m_a = $obj;
}
{
$m_a = $obj;
}
改为:
public function init($obj)
{
$this->m_a = $obj;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$m_aa改为$this->m_aa.
追问
$this->m_aa.一样的 all to a member function show on a non-object
追答
两处都要改,我这里运行没问提
m_aa = $obj;
}
public function test()
{
$this->m_aa->Show();
}
}
$bbb = new B();
$bbb->init(new A());
$bbb->test();
?>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询