Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    141
    Thanks
    3
    Thanked 1 Time in 1 Post

    Question OOP question call 2 function for one class? hah? help

    hi guys just wondering

    i never code this way and dont know the knowledge how this works

    say $test is a sample of foobar class

    $test = new foobar();

    $test->functionOne()->functionTwo()->functionThree();

    it looks like to me that functionThree return sometning to functionTwo which then return something to functionOne..

    or might be something else?

    can anyone give me a working example of how these functions are defined in the class?
    or let me know what is this programming technique called or something ? thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    There is no special technique here, simply short calls. functionOne() returns an object instance, functionTwo() returns an object instance, and functionThree returns an object instance. They may or may not be the same object.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    141
    Thanks
    3
    Thanked 1 Time in 1 Post
    so which one is returning to which in this case?

    functionThree returns to function two ?

    or the opposite function 2 returns to function 3 ?

    or even they all returns to the object of that class ?

    thanks

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ah sorry, I missed you were looking for an example on this.
    It works from left to right. Here is a quick example, its sorta infeasable but should show you what it does:
    PHP Code:
    class Foobar
    {
        public function 
    function1()
        {
            
    printf("%s\n"__METHOD__);
            return 
    $this;
        }
        public function 
    function2()
        {
            
    printf("%s\n"__METHOD__);
            return 
    $this;
        }
        public function 
    function3()
        {
            
    printf("%s\n"__METHOD__);
            return 
    $this;
        }
    }

    $fb = new Foobar();
    $fb->function1()->function2()->function3(); 
    So, calling $fb->function1(); returns a Foobar instance. That result can then be chained to immediately call function2() which also returns a Foobar instance, and then function3() would be called.

    This is generally not done on a single object, but may be done on properties within one. For a better example:
    PHP Code:
    class Foobar
    {
        private 
    $db;
        public function 
    __construct(DatabaseClassOfSomeKind $db)
        {
            
    $this->db $db;
        }

        
    // This one will return the DB stored, which according to the constructor is 
        // a type of DatabaseClassOfSomeKind.  We can then immediately use it.
        
    public function getDB()
        {
            return 
    $this->db;
        }
    }

    $fb = new Foobar(new Database());

    // These two are similar, except the assignment takes more memory:
    $db $fb->getDB();
    $db->connect();

    // And
    $fb->getDB()->connect(); 
    So, from you're last question, each method is required to return an object to be worked on. As soon as you see a -> in PHP, it means you're accessing either a member property or a method on an object (which also means that the previous method call in this example returned an object of some type).
    Hope that helps!

    Edit:
    I want to add as well, that PHP struggles on a similar context with static methods and members. It will likely throw a T_PAAMAYIM_NEKUDOTAYIM error at you.
    Last edited by Fou-Lu; 03-27-2009 at 03:25 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder
    Join Date
    Apr 2007
    Posts
    141
    Thanks
    3
    Thanked 1 Time in 1 Post
    So, from you're last question, each method is required to return an object to be worked on. As soon as you see a -> in PHP, it means you're accessing either a member property or a method on an object (which also means that the previous method call in this example returned an object of some type).
    Hope that helps!

    ok in your example getDB returns a "string of database name".

    that we use that "return of some type hehehe " to be used when we call connect() ?

    well . my skills is not that high yet. but could you give sample code of what connect() might contain ?


    because it appears not me that the database name is not passed to connect() function because connect() does not accept any parameter...

    -> accessing method ; i got it .. does that also mean we can do something like
    $fb->connect()->getDB(); ?


    thanks a lot!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sure:
    PHP Code:
    class DatabaseClassOfSomeKind
    {
        public function 
    connect()
        {
            
    $this->db = new MySQLi(...);
        }

    Simple example, this would just be a database wrapper of some kind, so it internally holds a database extension like MSSQL or MySqli for example.
    The chain is not returning a string representation of an object, its returning an actual object. This is why we can work on the results of the method call without it throwing an error.

    This: $fb->connect()->getDB(); would indicate that $fb (in my example, Foobar class) would have a connect() method within it, and the connect method would return an object that has a getDB() method on it. You cannot reverse the method calls, they have to go in order from left to right for evaluation. So my code block with $fb->getDB()->connect() would chain a Foobar method call to return a DatabaseClassOfSomeKind which can then be operated on using the connect method.

    Although its quite strong now, PHP is the newbie in the OO world. To learn an OO language, I'd recommend starting with Java, C# or C++ to really see some OO power.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder
    Join Date
    Apr 2007
    Posts
    141
    Thanks
    3
    Thanked 1 Time in 1 Post
    whow thanks! you are so GODly huahahahaa


  •  

    Posting Permissions

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