CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Syntax Woes

  1. #1
    Join Date
    May 2006
    Posts
    306

    Syntax Woes

    Okay, so I can't do this?

    PHP Code:
    <?php
        
    class { public function A() {} public function B() {} }

        new 
    A()->B();
    ?>
    How can I do this in one line?

    Also, what the difference between using __construct, and a function which has the same name as the class?

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Syntax Woes

    You can't do it in one line. First you have to create the object. Then after the object is created, then you may reference other methods contained within.

    As for your second questions...it was just asked recent in this very same forum.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    730

    Talking Re: Syntax Woes

    You can achieve something similar with a static constructor:

    PHP Code:
    class {
        public static 
    create()
        {
            return new 
    A();
        }

        public function 
    foo()
        {
            
    doSomething();
            return 
    $this;
        }

        public function 
    bar()
        {
            
    $result doOtherSomething();
            return 
    $result;
        }
    }

    $barResult A::create()->foo()->bar(); 
    All consequences are eternal in some way.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured