如何在 PHP 中使用内部类?
我来自Java背景,我想在php中使用内部类。但每次我放置内部类时,都会出现语法错误。这可以用 PHP 实现吗?另外,如何引用外部类?我可以访问其所有数据成员吗?
<?php
class OuterClass {
var $x = 15;
function __construct() {
}
class InnerClass { // error when InnerClass is static
function __construct() { // error when InnerClass is static
echo $x;
}
}
}
?>
这用于特定纸牌游戏的 MoveClass(如移动)。我认为将这些类放在一起是一个很好的设计,因为它们分开没有意义。此外,MoveClass 需要了解 Game 类的一些数据成员。为什么不把它变成一个函数呢?实在是太大了。
编辑:
嵌套类怎么样?据我了解,那些必须是静态的?奥奥
I'm from a Java background, and I want to use an inner class in php. Every time I put the inner class though, I get a syntax error. Is this possible with PHP? Also, how do I reference the outer class? Do I get access to ALL its data members?
<?php
class OuterClass {
var $x = 15;
function __construct() {
}
class InnerClass { // error when InnerClass is static
function __construct() { // error when InnerClass is static
echo $x;
}
}
}
?>
This is used for a MoveClass (as in make a move) of a specific card game. I think it'd be good design to put these classes together because they don't make sense apart. Also, the MoveClass needs to know about some data members of the Game class. Why not make it a function? It's simply too big.
Edit:
What about nested classes? From what I understand, those have to be static? O_o
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
PHP 不允许内部类。如果您希望访问父类的所有数据成员,我建议您使用继承。
一个可能的替代方案:
您可以通过引用类本身来调用父类的方法;在 PHP 中,您可以使用
parent
关键字来完成此操作。因此,要在类上下文中引用方法而不是对象,我们使用::
而不是->
。PHP does not allow for inner classes. Should you wish to access all of the data members from the parent class, I would suggest you employ Inheritance.
A possible alternative:
You can envoke a method form the parent class by referring to the class itself; In PHP you can do this with the
parent
keyword. So, to refer to a method in the context of a class rather than an object we use::
as opposed to->
.在 PHP 5.4 或更高版本中,您可以使用 PHP Traits,它们更多地为多重继承而设计,但可能适合您的需求。来自 PHP 文档:
http://php.net/manual/en/language.oop5.traits.php
in PHP 5.4 or later, you can use PHP Traits which are designed more for multiple inheritance, but may suit your needs. From the PHP Documentation:
http://php.net/manual/en/language.oop5.traits.php
正如此答案的评论中所述,来自另一个问题,该功能的PHP版本已在PHP 7中添加。它似乎没有完全提供您所要求的功能。但是,它应该为您提供类似的设计模式。
以下是描述其工作原理的 RFC:https://wiki.php.net/rfc/anonymous_classes
正如其他答案的评论中提到的,在 RFC 页面中搜索“nested”以查看嵌套在外部类中的示例。
As mentioned in comments to this answer from another question, the PHP version of this functionality has been added in PHP 7. It does not seem to provide exactly what you are asking for. However, it should provide you a similar design pattern.
Here's the RFC describing how it works: https://wiki.php.net/rfc/anonymous_classes
As mentioned in the other answer's comments, search the RFC page for "nested" to see an example of nesting inside an outer class.
您可能想改用 stdClass。这是一个关于它的问题: 什么是 PHP 中的 stdClass?
You might want to use a stdClass instead. Here's an SO question about it: What is stdClass in PHP?
你不能像这样嵌套类。
请参阅手册的“扩展”部分。
You can't nest classes like that.
Look at the "extends" section of the manual.