8

Does PHP versions 5.3 or after support inner classes? example:

class MyClass{
    class PrivateClass1{

    }
    class PrivateClass2{

    }
    class PrivateClass3{

    }

    private $obj1;
    private $obj2;
    private $obj3;

    __construct(){
        $obj1 = new PrivateClass1();
        $obj2 = new PrivateClass2();
        $obj3 = new PrivateClass3();
    }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Johntor
  • 587
  • 11
  • 26
  • 1
    In which version did you try this? – Lion May 20 '12 at 18:39
  • 1
    What is the purpose of an inner class? And what other languages support them? I'm intrigued. – Mark Baker May 20 '12 at 18:41
  • No php does not support nested classes. @Lion I dont think he tried. It contains lots of errors. no `$` sign. no `function` keyword for method declaration – Shiplu Mokaddim May 20 '12 at 18:42
  • This looks like a Jaca snippet, if I may believe [this bugreport](https://bugs.php.net/bug.php?id=51469) inner classes are not yet supported. – Lekensteyn May 20 '12 at 18:42
  • possible duplicate of [is it allowed to create a php class inside another class](http://stackoverflow.com/questions/1583140/is-it-allowed-to-create-a-php-class-inside-another-class) – hakre May 20 '12 at 18:45
  • @MarkBaker C# supports them... VB supports them. ALL.NET support them – Cole Tobin May 20 '12 at 18:45
  • 1
    @Cole Johnson: dotnet ain't no PHP not at all. – hakre May 20 '12 at 18:46
  • @hakre I didn't say it was. I was answering this: "What is the purpose of an inner class? And what other languages support them?" – Cole Tobin May 20 '12 at 18:48
  • I haven't tried but I would like to!!! Thank you very much for your answers. I need it to do so I asked – Johntor May 20 '12 at 18:48
  • 3
    @Mark Baker Java supports inner classes – Johntor May 20 '12 at 18:49
  • @Johntor:) Java supports inner classes but it has whatsoever nothing to do with PHP as obvious. – Lion May 20 '12 at 18:53
  • @Lion yes but PHP is getting better and better!! When I started there was no OOP support at all!! – Johntor May 20 '12 at 18:54
  • Well it answers the second part of my question, still unsure of their purpose – Mark Baker May 20 '12 at 19:19
  • At least for me, I use inner classes when I want to use them 'privately' into my class only. So I make them and put them into one file nicely and tidy! – Johntor May 20 '12 at 19:27

2 Answers2

13

PHP currently (5.4.3) does not support Inner/Friend Classes

And there is also no RFC in the wiki asking for addition of a feature like this.

Gordon
  • 312,688
  • 75
  • 539
  • 559
2

It's possible to create a class within another class definiton, but it's not possible to define a class within a class definition. So this means your construction in invalid.

But, you can always extend a class with another class, check this URL for more:

http://php.net/manual/en/keyword.extends.php

Check Can I instantiate a PHP class inside another class? for even more.

Community
  • 1
  • 1
Sliq
  • 15,937
  • 27
  • 110
  • 143
  • Sorry I know how to extend a class. I need to create something like this example – Johntor May 20 '12 at 18:52
  • @Johntor Yes i think someone who asks for class nesting knows about extending, I just wanted to give an alternative for people who read this thing in future. – Sliq May 20 '12 at 18:55