SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    Embedded classes

    Hey all,

    Can you have a class inside a class in PHP5? I'm trying with this code
    PHP Code:
    <?php
    class Foo {
        class 
    Bar {
            public 
    $test;
            
            function 
    __construct($var) {
                
    $this->test $var;
            }
            
            function 
    ToString() {
                echo 
    $this->test;
            }
        }
    }
    ?>
    But I'm getting an error, unexpected class, expecing function. Am I doing this wrong or can't it be done?

    Thanks

  2. #2
    SitePoint Zealot
    Join Date
    Oct 2002
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why would you want to? lol

  3. #3
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the outer class is the only class to ever use the inner class. Normally, this SHOULD be handled with namespaces, as subclasses are one of these things that can make your code very messy, but PHP5 doesn't do those (hows that for a frickin' brilliant decision - version 5 was turning out so well...)

    I don't know about subclasses, though.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  4. #4
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I remember correctly PHP5 was going to include nested classes, but they were removed and abandoned along with Namespaces.

  5. #5
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What? When did they remove namespaces? I have like 5 namespaces in one of the scripts i am saving for the full release! AGGGGG
    Ryan

  6. #6
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Gybbyl
    What? When did they remove namespaces? I have like 5 namespaces in one of the scripts i am saving for the full release! AGGGGG


    Well, the plan was to use nested classes to try and simulate namespaces but I guess that's out the window too

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    North Carolina, USA
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Um, am I missing something here? Can't you just use inherited classes to acheive the same effect here? *is confused*

  8. #8
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, namespaces can hold multiple classes inside them, they can even include functions, variables, and plain code -- They essentially just create a kind of 'artificial scope' (namespace scope i think is what they call it ;] ).

    You can import the classes and such from the namespaces -- Anyway, it's really good for seperating things and grouping things (not necessarily at the same time).

    The classes within the namespace aren't necessarily inheriting from one another, although they could be passing instances of themselves to each other. I guess it might be kind of like a nested class, except that you can't make an instance of a namespace.

    --- Off topic:
    Arg, the next thing they are going to tell me is that they removed Interfaces and abstract classes from their spec. That would just put me over the edge.
    Ryan

  9. #9
    SitePoint Zealot
    Join Date
    Aug 2002
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The namespace RIP was announced here: http://marc.theaimsgroup.com/?l=php-...6426332183&w=2

    As for interfaces and abstract classes: they are in the beta that just came out and won't be taken away!

  10. #10
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good, or else I would have to go .NET on those fools. I suppose I could probably just read the changelog and 'keep informed' of all these details. Good plan, I'll do that now, so there will be no more surprises.
    Ryan

  11. #11
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IT really is a shame that they removed name spaces IMHO, they can be very handy when working with large code bases, or in projects where you're using multiple other projects to form an overall one.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  12. #12
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Completely agree. There were two arguments for removing namespaces, I believe:
    - the import statement didn't work like people used to the Java import statement would expect it to work
    - "when creating a variable, how do you know where to create it?"

    About no. 1: PHP is not java, so what kind of argument is that. And about no. 2: call me stupid, but how about creating them in the current namespace?

    Ah well, back to renaming all of my PHP 5 classes with a package prefix

  13. #13
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think the exact issues with namespaces was highlighted here.

    The problems with namespaces were not 'how do we locate this class on disk', but how to implement scoping and scope importing in logical, efficient and easy-to-use way. The decision was reached that the
    way we have is not ripe for putting it into the language, so it was removed.
    That I can accept (the original annoucement didn't make it clear) if I read it as "keeping track of namespaces at runtime, the way we had them implemented, was too much overhead". In other words they dropped what they thought was a bad design and don't have a replacement right now but don't want to hold up the PHP5 release while trying to get it right.

  14. #14
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Captain Proton
    About no. 1: PHP is not java, so what kind of argument is that. And about no. 2: call me stupid, but how about creating them in the current namespace?
    Completey agreed on that point, PHP is not Java so who cares if the import worked in a different way?
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  15. #15
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah. I find it weird, I've read the developers quote "PHP != Java" so many times, yet they complain about the import statement being confusing to people who might expect it to work like Java does.

    Ah well, having to live without namespaces is not the worst thing to happen I think. I mean, I'd say we've been coping with the lack of namespaces in the current PHP versions pretty good so far

  16. #16
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Lol, it was like holding a piece of meat in front of a dog and then yanking it away.

    They were just teasing

  17. #17
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem wasn't that the import command was 'confusing' but that it simply didn;y work the way they wanted it to, which would have drastically limited how useful namespaces could actually be.

    I think the simple fact is that the architecture of Zend Engine 2 just isn't up to the demands of structures like this.

  18. #18
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Phil.Roberts
    I think the simple fact is that the architecture of Zend Engine 2 just isn't up to the demands of structures like this.
    Good point, PHP has never been up to scratch compared to C#/Java in the OOP department, and while Zend2.0 is a major upgrade, maybe it's still not enough for people to be implementing this sort of thing.

    Lets all sit back and wait for Zend3.0


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •