需要帮助理解 php 对象的各个方面
我是 php 新手,需要一些帮助来理解我不明白的 php 对象的各个方面。在类ai中已经创建了类b和c,它们扩展了a,但我想做的是从类c访问类a中的公共字母变量,这样就可以从类c获取函数testb()。
非常感谢任何帮助,
<?php
class a {
public $letters;
function __construct() {
$this->letters->b = new b();
$this->letters->c = new c();
}
}
class b extends a {
function __construct() {
echo "hello world from b ";
}
private function testb() {
echo "testing from b";
}
}
class c extends a {
function __construct() {
echo "hello world from c ";
$this->letters->b->testb();
}
}
$a = new a();
?>
以下脚本呼应了我们的“来自 b 的 hello world”和“来自 c 的 hello world”,但它不会打印出“来自 b 的测试”...
i am new to php and is in need of some help to understand the aspects of php objects which i do not get. In class a i have made class b and c which extends a, but what i want to do is acess the public letters variable in class a from class c so in can get ot the function testb() from class c.
Any help is much appreciated guys
<?php
class a {
public $letters;
function __construct() {
$this->letters->b = new b();
$this->letters->c = new c();
}
}
class b extends a {
function __construct() {
echo "hello world from b ";
}
private function testb() {
echo "testing from b";
}
}
class c extends a {
function __construct() {
echo "hello world from c ";
$this->letters->b->testb();
}
}
$a = new a();
?>
the following script echos our "hello world from b" and "hello world from c" but it does not print out "testing from b"...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
PHP 对象的一个问题是继承被捏造了(只是一点点)。当您实例化其中一个子对象时,不会隐式调用父对象的构造函数。相反,您必须显式调用父对象的构造函数。
在类
c
构造函数中,$this->letters
不是对象,并且尚未初始化,因为尚未调用a
构造函数。如果您只是在玩弄对象,那么请忽略这部分,但您已经接近获得循环层次结构了。如果您添加我建议的更改,我相信如果您尝试直接实例化 B 或 C 对象,您的代码将会爆炸。
A gotcha of PHP Objects is that Inheritance is fudged (just a bit).. The constructor of a parent object is not implicitly called when you instantiate one of it's child objects. Instead you must explicitly call the Parent Object's constructor.
In your Class
c
constructor,$this->letters
is not an object and has not been initialized since thea
constructor has not been called.If you're just playing around with objects, then ignore this part, but you're close to getting a circular hierarchy. If you add the changes that I suggested, I believe your code will explode if you try to instantiate either a B or a C object directly.
您无法从类
c
中调用testb
的原因是testb
的方法签名将其声明为private - 意味着它只能在
b
实例中访问。将可见性更改为默认(无修饰符)或public
。例如:
或者:
编辑:
问题是您没有从 c 的构造函数中调用超级构造函数 - 因此
$this->b
的值未初始化。将 c 的构造函数更改为:但是不幸的是,这将创建一个无限循环,其中
a
的构造函数正在实例化c
和c
的构造函数> 正在调用a
的构造函数。为什么你们的字母类别会相互延伸?您还需要更改
b
类中testb
的可见性。The reason you can't call
testb
from within the classc
is that the method signature oftestb
declares it asprivate
- meaning it can only be accessed withinb
instances. Change the visibility to default (no modifier) orpublic
.E.g.:
Or:
Edit:
The problem is that you haven't called the super constructor from the constructor of c - the value of
$this->b
is therefore not initialised. Change the constructor of c to:Unfortunately, though, this will create an infinite loop where the constructor of
a
is instantiatingc
and the constructor ofc
is calling up to the constructor ofa
. Why do your letter classes even extend one another?You also need to change the visibility of
testb
within theb
class.好的,如果您从 b 扩展类 c ,您可以调用该方法,否则不是因为您是从类 a 而不是从 c 扩展它。
Ok, if you extend class c from b you can call the method otherwise not as you are extending it from class a not from c..
在类
a
中,您正在构造类c
的新对象。这个新对象没有属性
$this->letters->b
,它只有一个未初始化的属性$this->letters
。请注意,新对象从其扩展的类继承了属性
$letters
,但它不继承实例化a
对象的任何值。所以没有$this->letters->b
也没有函数$this->letters->b->testb()
可以调用。In your class
a
you are constructing a new object of classc
.This new object has no property
$this->letters->b
, it just has an uninitialized property$this->letters
.Note that the new object inherits the property
$letters
from the class it extends, but it does not inherit any values of an instantiateda
object. So there is no$this->letters->b
and no function$this->letters->b->testb()
to call.“c”扩展了“a”并且不继承
您的示例将返回的
上
"c" extends "a" and doesn't inherit "b"s method
your example will return
for