如何在 PHP 中使用内部类?

发布于 2024-10-06 03:54:41 字数 556 浏览 0 评论 0原文

我来自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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

卷耳 2024-10-13 03:54:41

PHP 不允许内部类。如果您希望访问父类的所有数据成员,我建议您使用继承。

一个可能的替代方案:

class OuterClass {
    var $x = 15;
    function __construct() {

    }
}
class ChildClass extends OuterClass {
    function __construct() {
        parent::__construct();
    }
}

您可以通过引用类本身来调用父类的方法;在 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:

class OuterClass {
    var $x = 15;
    function __construct() {

    }
}
class ChildClass extends OuterClass {
    function __construct() {
        parent::__construct();
    }
}

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 ->.

旧情勿念 2024-10-13 03:54:41

在 PHP 5.4 或更高版本中,您可以使用 PHP Traits,它们更多地为多重继承而设计,但可能适合您的需求。来自 PHP 文档:

Trait 与类类似,但仅用于分组
以细粒度且一致的方式提供功能。这是不可能的
自行实例化一个 Trait。它是对传统的补充
继承并实现行为的水平组合;那是,
无需继承即可应用类成员。

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:

A Trait is similar to a class, but only intended to group
functionality in a fine-grained and consistent way. It is not possible
to instantiate a Trait on its own. It is an addition to traditional
inheritance and enables horizontal composition of behavior; that is,
the application of class members without requiring inheritance.

http://php.net/manual/en/language.oop5.traits.php

路还长,别太狂 2024-10-13 03:54:41

正如此答案的评论中所述,来自另一个问题,该功能的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.

迷爱 2024-10-13 03:54:41

您可能想改用 stdClass。这是一个关于它的问题: 什么是 PHP 中的 stdClass?

You might want to use a stdClass instead. Here's an SO question about it: What is stdClass in PHP?

黎歌 2024-10-13 03:54:41

你不能像这样嵌套类。

请参阅手册的“扩展”部分。

You can't nest classes like that.

Look at the "extends" section of the manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文