Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-27-2009, 01:01 AM   PM User | #1
likon
Regular Coder

 
Join Date: Apr 2007
Posts: 141
Thanks: 3
Thanked 1 Time in 1 Post
likon is an unknown quantity at this point
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
likon is offline   Reply With Quote
Old 03-27-2009, 02:10 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-27-2009, 03:14 AM   PM User | #3
likon
Regular Coder

 
Join Date: Apr 2007
Posts: 141
Thanks: 3
Thanked 1 Time in 1 Post
likon is an unknown quantity at this point
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
likon is offline   Reply With Quote
Old 03-27-2009, 03:22 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 03-27-2009 at 03:25 AM..
Fou-Lu is offline   Reply With Quote
Old 03-27-2009, 03:48 AM   PM User | #5
likon
Regular Coder

 
Join Date: Apr 2007
Posts: 141
Thanks: 3
Thanked 1 Time in 1 Post
likon is an unknown quantity at this point
Quote:
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!
likon is offline   Reply With Quote
Old 03-27-2009, 03:57 AM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-27-2009, 04:05 AM   PM User | #7
likon
Regular Coder

 
Join Date: Apr 2007
Posts: 141
Thanks: 3
Thanked 1 Time in 1 Post
likon is an unknown quantity at this point
whow thanks! you are so GODly huahahahaa
likon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:12 AM.


Advertisement
Log in to turn off these ads.