Is it possible to create nested classes in PHP like in C #?

In C #, you can have nested classes that are useful if you have classes that don't matter outside the scope of one particular class, for example. in the factory template:

public abstract class BankAccount { private BankAccount() {} private sealed class SavingsAccount : BankAccount { ... } private sealed class CheckingAccount : BankAccount { ... } public BankAccount MakeSavingAccount() { ... } public BankAccount MakeCheckingAccount() { ... } } 

Is this possible in PHP?

I read that it was planned for PHP 5, then canceled, then planned again, but cannot find the final information.

Does anyone know how to create nested classes (classes within another class) like in the above C # example using PHP 5.3?

+4
source share
1 answer

No, this is not possible in PHP. Classes must be in the global namespace; the best you can do is just pretend they don't exist in all of your other code.

This view is common in PHP; for example, PHP has no static initializers, so if you want to eagerly initialize static members of a class, this should be done by calling a public method from outside the class. Ugly.

+1
source

Source: https://habr.com/ru/post/1335535/


All Articles