TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-02-2008, 06:16 PM   #1 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default 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?
__________________
Signatures are nothing but incriminating.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 08-02-2008, 06:39 PM   #2 (permalink)
The Wanderer
 
Join Date: May 2008
Posts: 17
Thanks: 2
Alex is on a distinguished road
Default

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.
Alex is offline  
Reply With Quote
Old 08-03-2008, 12:13 AM   #3 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Ahh, so it would work?

Thanks for your help.
__________________
Signatures are nothing but incriminating.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 08-04-2008, 01:46 PM   #4 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default

Don't know if you've already read it, this article would be nice for you to read: Using the factory pattern (mad rantings of a mind without coffee)

The Factory pattern

edit: Wtf, why does ^ ^ together change into ? :(
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Old 08-05-2008, 07:01 AM   #5 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

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

You can disable smily's though, can't you?
__________________
Signatures are nothing but incriminating.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:07 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design