如何在 PHP 中使用内部类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4351782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:46:10  来源:igfitidea点击:

How do I Use Inner Classes in PHP?

phpclassinner-classes

提问by Lindz

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?

我来自 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;
        }
    }
}

?>

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.

这用于特定纸牌游戏的 MoveClass(如进行移动)。我认为将这些类放在一起是一个很好的设计,因为它们分开没有意义。此外,MoveClass 需要了解 Game 类的一些数据成员。为什么不让它成为一个函数?它简直太大了。

Edit:

编辑:

What about nested classes? From what I understand, those have to be static? O_o

嵌套类呢?据我了解,那些必须是静态的?o_o

回答by Russell Dias

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.

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

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 parentkeyword. So, to refer to a method in the context of a class rather than an object we use ::as opposed to ->.

您可以通过引用类本身来从父类调用方法;在 PHP 中,您可以使用parent关键字执行此操作。因此,要在类的上下文中引用方法而不是我们使用的对象::,而不是->.

回答by Jason

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:

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

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.

Trait 类似于类,但仅用于以细粒度和一致的方式对功能进行分组。不可能单独实例化 Trait。它是对传统继承的补充,可以实现行为的横向组合;也就是说,类成员的应用不需要继承。

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

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

回答by jason

As mentioned in comments to this answerfrom 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.

正如在评论中提到这个答案另一个问题,这个功能的PHP版本已经在PHP 7.添加它似乎没有提供你问什么了。但是,它应该为您提供类似的设计模式。

Here's the RFC describing how it works: https://wiki.php.net/rfc/anonymous_classes

这是描述其工作原理的 RFC: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.

如其他答案的评论中所述,在 RFC 页面中搜索“嵌套”以查看在外部类中嵌套的示例。

回答by mqsoh

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

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

回答by simshaun

You can't nest classes like that.

你不能嵌套这样的类。

Look at the "extends" section of the manual.

查看手册的“扩展”部分。