SitePoint Sponsor |
|
User Tag List
Results 26 to 42 of 42
-
May 18, 2008, 10:33 #26
- Join Date
- Feb 2007
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks much, kyberfabrikken.
I tried to implement your suggestions and came up with the following...
PHP Code:interface IDbConnection {
public function prep($query);
public function exec($query, $data = null);
}
class DbConnection implements IDbConnection {
private $db;
public function __construct($host, $dbname, $user, $pass) {
$this->db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
public function prep($query) {
return $this->db->prepare($query);
}
public function exec($query, $data = null) {
$stmt = $this->prep($query);
$stmt->execute($query, $data);
return $stmt;
}
}
class DbConnectionDecorator implements IDbConnection {
private $decoratedDb;
public function __construct(IDbConnection $decoratedDb) {
$this->decoratedDb = $decoratedDb;
}
public function prep($query) {
return $this->decoratedDb->prep($query);
}
public function exec($query, $data = null) {
return $this->decoratedDb->exec($query, $data);
}
}
class QueryCache extends DbConnectionDecorator {
private $stmts = array();
public function prep($query) {
if (isset($this->stmts[$query])) {
return $this->stmts[$query];
}
$this->stmts[$query] = $this->decoratedDb->prep($query);
return $this->stmts[$query];
}
}
class ThingLogic {
private $db;
public function __construct(IDbConnection $db) {
$this->db = $db;
}
public function addThing($data) {
$this->db->exec('INSERT INTO...', $data);
}
}
class SiteController {
private $db;
public function __construct(IDbConnection $db) {
$this->db = $db;
}
public function process(Request $request) {
$module = $request->getModule();
if (method_exists($this, $method = 'process'.ucwords($module)) {
$this->$method($request);
} else {
throw new InvalidModuleException("Module $method does not exist");
}
}
private function processThing(Request $request) {
$controller = new ThingController(new ThingLogic($this->db));
$controller->process($request);
}
}
$site = new SiteController(new QueryCache(new DbConnection('host', 'db', 'user', 'pass')));
$site->process(new Request(array('module' => 'thing', 'action' => 'add')));
I must admit this seems to be quite a bit purtier.
Bonus: it's the first time I've ever used the decorator pattern outside of assigning toppings to virtual hamburgers!Last edited by cuberoot; May 18, 2008 at 11:52.
-
May 19, 2008, 05:14 #27
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Thanks arkinstall -- clearest explanation I've seen to dateJake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
May 19, 2008, 05:56 #28
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Perhaps you could be clearer about this:
# A class is practically a set of variables and methods.
# An object is a variable which uses the instructions (vars and methods) laid out by the class.
-
May 19, 2008, 06:40 #29
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Yeah, I will include that.
The point I will get across mainly (because I have seen so much misunderstanding about it from beginners) is that a class is simply a source of instructions for the object - the functions and variables of a class don't actually exist, unless they're in an object, as attributes and methods unique to that object.
It's a set of instructions, and that class can do nothing but follow that set of instructions.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
May 19, 2008, 08:07 #30
- Join Date
- Mar 2008
- Location
- NL, Rotterdam
- Posts
- 24
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
As for the original question as to why such a programming concept is called a 'class': Take the real world example ship classes...
http://en.wikipedia.org/wiki/Ship_class
-
May 19, 2008, 08:47 #31
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 19, 2008, 09:21 #32
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
In more advanced OOP-languages ... "class" is just an object like everything else.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
May 19, 2008, 09:44 #33
- Join Date
- Sep 2006
- Posts
- 232
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 19, 2008, 10:45 #34
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sure.
Code:# ruby # declare a class class A end # A is a constant of type Class puts A puts A.class # b is a variable of type Class b = A # we can call methods on b puts b.methods puts b.superclass # 'new' is just a method in class Class obj1 = A.new obj2 = b.new
http://ruby-doc.org/core-1.8.3/classes/Class.html
-
May 19, 2008, 16:39 #35
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Somebody already yelled "NOOOO", but let me elaborate on that.
It is a most dangerous thing to include code directly into a method's (yes, functions of an object are called methods) code. The whole point of methods (and of regular functions too, but in OOP this is even more pronounced) is to isolate a piece of code and ensure the stability by providing a limited number of entry and exit points, ideally one of each or less.
Instead of the above, you could have a separate Configuration object(s) which would be passed as an argument to the function that requires them:PHP Code:
class Foo
{
function Bar($config)
{
$this->indexsize = $config->indexsize;
$this->entriesperpage = $config->entriesperpage;
}
}
-
May 20, 2008, 08:29 #36
- Join Date
- Sep 2006
- Posts
- 232
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
A nice article about Object-Oriented Programming with PHP
-
May 20, 2008, 09:09 #37
-
May 20, 2008, 10:00 #38
- Join Date
- Nov 2004
- Location
- Lincoln Nebraska
- Posts
- 1,161
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you have a class that represents a database connection, would it be that bad to set the connection details at the class level rather then the instance level? Most of the time, you aren't going to use multiple connections, you would instead pass the connection object to objects that need to access a database, but in case you needed multiple connections, what would really be wrong with it?
-
May 20, 2008, 10:19 #39
- Join Date
- Sep 2003
- Location
- Wixom, Michigan
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What would be wrong with it is the fact that in a design where database connection information is kept in static class variables (such as the one you mention), you would not be able to change the configuration of one connection without affecting all the others, since they all share the same static properties.
Garcia
-
May 20, 2008, 10:30 #40
- Join Date
- Nov 2004
- Location
- Lincoln Nebraska
- Posts
- 1,161
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 23, 2008, 01:34 #41
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well yes, I am sure -- I'm just not sure how did you understand it.
Consider this: If you're including external code into a class, that means that you have some piece of code in a separate file. But does it really have to be in a separate file?
If you're using this code only on that one place where it's included, there is absolutely no reason why it shouldn't be put directly at the class definition instead of being included. If this code is used in several places, that it is most logical to keep it separate -- but it is still beneficial if it is properly isolated into a class or at least a function of its own and then called from the methods that need it.
You can think about your code as about electronics equipment. if you have non-isolated, included code it's like a bare electronic circuit which can be inserted anywhere, but each time you have to make sure that you have correctly placed all the connections and soldered all the wires. If your code is properly isolated it's like a hardware module which can easily be plugged in or out at will.
Isolation is a great help when maintaining applications, and its greater as apps become more complex. Imagine a situation where you have to change something in the included code in order to make it work in the case A, only to discover (maybe weeks later) that it broke something in the case B. If the code was properly isolated it would be simple to alter the function's signature to make the exception when used in the case A and working the same as before from the case B. You can even make separate function for each case and keep a switcher function which is actually called by the classes.
-
May 26, 2008, 07:45 #42
- Join Date
- Sep 2003
- Location
- Wixom, Michigan
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the clarification, I didn't realize you were referring to using the "include" directive within a class method. I agree with you, this is something I would never do; the results would be too unpredictable.
Garcia
Bookmarks