TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Is this OO Possible? (Method Chaining) (http://www.talkphp.com/advanced-php-programming/3203-oo-possible-method-chaining.html)

Aaron 08-02-2008 06:16 PM

Is this OO Possible? (Method Chaining)
 
PHP Code:

class MyClass
{
  public function 
instSql()
  {
    
$sql = new Sql;
  }
}
class 
Sql
{
  private function 
Sql() // Constructor
  
{
    
// Connect to the database, blah blah blah
    
return $this;
  }

  public function 
Query()
  {
    
// Send a query
    
return $result;
  }
}


// Later...

$cl = new MyClass;
$cl->instSql->Query(); 

Uhh... That kind of got confusing... Anyway, would something like this work?

Alex 08-02-2008 06:39 PM

PHP Code:

class MyClass 

  public function 
instSql() 
  { 
    return new 
Sql
  } 

class 
Sql 

  private function 
Sql() // Constructor 
  

    
// Connect to the database, blah blah blah 
    
return $this
  } 

  public function 
Query() 
  { 
    
// Send a query 
    
return $result
  } 



// Later... 

$cl = new MyClass
$cl->instSql()->Query(); 

The instSql() should return an instance of an object, then you can 'chain' the calls.

Aaron 08-03-2008 12:13 AM

Ahh, so it would work?

Thanks for your help.

Jim 08-04-2008 01:46 PM

Don't know if you've already read it, this article would be nice for you to read: http://www.talkphp.com/advanced-php-...ut-coffee.html

The Factory pattern ^^

edit: Wtf, why does ^ ^ together change into ^^? :(

Aaron 08-05-2008 07:01 AM

Jim, you always find the most dusty yet amazing articles. Thanks a lot man.

You can disable smily's though, can't you?


All times are GMT. The time now is 09:26 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0