OO PHP引用类的实例 [英] OO PHP Referencing an instance of a class
问题描述
好的,所有我在课堂上写的一些问题是希望你们所有人都能帮助你们。我没有访问这里的实际代码,但生病写了
我想尝试做的一个例子,也许你们都可以提供
一些指示。
1:class Foo
2:{
3:$ mInstanceBar;
4:
5:函数__construct()
6:{
7:$ this-> mInstanceBar =&新酒吧();
8:}
9:
10:函数callBarHelloWorld()
11: {
12:$ this-> mInstanceBar-> HelloWorld();
13:}
14:}
15:
16:班级酒吧
17:{
18:函数HelloWorld()
19:{
20:echoHello World!;
21:}
22:}
23:$ foo = new Foo();
24:$ foo-> callBarHelloWorld();
Ok line 12返回错误说一些东西,即$
在不是对象的东西上调用函数。
我的印象是在第7行我实例化对象作为一个
参考,在第12行调用时仍然可用。
所以任何想法?
Ok all im havign some issues with a class im writing was hoping you all
could help. I dont have access to the actual code here but ill write
out an example of what im trying to do and maybe you all could provide
some pointers.
1: class Foo
2: {
3: $mInstanceBar;
4:
5: function __construct()
6: {
7: $this->mInstanceBar = & new Bar();
8: }
9:
10: function callBarHelloWorld()
11: {
12: $this->mInstanceBar->HelloWorld();
13: }
14: }
15:
16: class Bar
17: {
18: function HelloWorld()
19: {
20: echo "Hello World!";
21: }
22: }
23: $foo = new Foo();
24: $foo->callBarHelloWorld();
Ok line 12 returns an error saying something to the effect that im
calling a function on something that is not an object.
My impression was that on line 7 i am instantiating the object as a
reference which should still be available when called on line 12.
So any ideas?
推荐答案
mInstanceBar;
4:
5:函数__construct()
6:{
7:
mInstanceBar;
4:
5: function __construct()
6: {
7:
this-> mInstanceBar =&新酒吧();
8:}
9:
10:函数callBarHelloWorld()
11: {
12:
this->mInstanceBar = & new Bar();
8: }
9:
10: function callBarHelloWorld()
11: {
12:
this-> mInstanceBar-> HelloWorld();
13:}
14:}
15:
16:班级酒吧
17:{
18:功能HelloWorld()
19:{
20:echoHello World!;
21:}
22:}
23:
this->mInstanceBar->HelloWorld();
13: }
14: }
15:
16: class Bar
17: {
18: function HelloWorld()
19: {
20: echo "Hello World!";
21: }
22: }
23:
这篇关于OO PHP引用类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!