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 11-02-2010, 12:08 AM   #1 (permalink)
The Wanderer
 
Join Date: Nov 2010
Posts: 5
Thanks: 0
jwilson122 is on a distinguished road
Help class?

Hey, I was wondering if anyone could give me an example script or show me how to make a class like such:
PHP Code:
$do->Query("SELECT * FROM users")->Where("id = 1")->extra("LIMIT 1"); 
See, I know how to make a class and functions inside of the class to make:
PHP Code:
$do->Query("SELECT * FROM users");
$do->Where(""id 1");
$do->extra("LIMIT 1"); 
But I would rather have the single line link..
PHP Code:
$do->Query("SELECT * FROM users")->Where("id = 1")->extra("LIMIT 1"); 
Is this considered OOP (object oriented programming) ? I've searched and searched for this, tried many things.. nothings working :/ any help? Thanks!
jwilson122 is offline  
Reply With Quote
Old 11-02-2010, 01:22 AM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

that is call method chaining. If the methods Query, Where, and extra (and whichever other method that needs chaining) you just need to return the current instance of the class that $do is an instance of. for example, at the end of the Query method just put, return $this;

there is a tutorial/article here
tony is offline  
Reply With Quote
Old 11-02-2010, 02:18 AM   #3 (permalink)
The Wanderer
 
Join Date: Nov 2010
Posts: 5
Thanks: 0
jwilson122 is on a distinguished road
Default

Would you guys recommend just doing like..
PHP Code:
$_db->query("SELECT * FROM bla bla"); 
?? Or making a class chain?
jwilson122 is offline  
Reply With Quote
Old 11-02-2010, 05:21 AM   #4 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 7
Thanks: 0
jgetner is on a distinguished road
Default

ether or is fine really depends on your needs for the application you are constructing.

here is a great article about here on TP

PHP5 Method Chaining
jgetner is offline  
Reply With Quote
Old 11-02-2010, 09:45 AM   #5 (permalink)
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Default

I would have done the same as Tony.
A class with methods "Query", "Where", "Limit", etc... + a property "Results" pointing on a private collection storing the results of the query.
The method "Query" puts the results of the query in the private collection and then, the methods "Where" and "Limit" make the limitations in the collection.

The bad thing in that is that it can returns a lot of unnecessary data but for the case you don't want to apply a "where" clause after the "Query", it's ok.
__________________
Wedus project's Website
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote
Old 11-02-2010, 03:28 PM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Even if you can get this working it is a bad idea, SQL is SQL and PHP is PHP; dont mix them. There is no valid reason to be defining parts of your query like that, it can create bugs and severely limits your ability to write clean queries queries if they are to get complex. The default MySQL library for PHP is bad enough as is, dont make it worse.
__________________

Village Idiot 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
base classes..... allworknoplay Absolute Beginners 16 05-10-2009 08:09 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 07:43 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