Well, it's good and bad. Good because you don't risk harming/changing the original data, but bad because it's less efficient. I'm fine with it in C++ because you can easily turn any variable into a reference, though ampersands everywhere tends to look messy. Maybe they should have put it the other way around.ole wrote:PHP 4 and C++ both copy and are both ridiculous for it. Most languages reference including PHP 5.I believe... C++ makes copies, but I think PHP creates references
Database wrapper class
Moderator: General Moderators
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Code: Select all
str_replace('/', DIRECTORY_SEPARATOR, 'foo/bar/whatever.php');Negative. I probably should, but I'm content with my own coding style. When I get good at this stuff, and writing something other people may want to contribute on or work with, then I'll learn the coding standards.ole wrote:Also have you considered following Zend or PEAR coding standards? See the first link in my sig.
Although, I've read the standards before. I could do them but I just choose not to. For now I like to stick with underscores and all lowercase letters.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
A couple of comments.
1. As for isError() and getError() they would return true/false or a string respectively. They are used to check if an error occurred by saving the error state after the query or fetch.
2. I prefer to always return an object from the query() method. It makes it consistent to deal with rather than it being either an object or false. Especially if you pass it, then the dependency is clean/clear.
3. I would recommend that you change the naming now. It is not a good practice become too familiar with you own non-standard naming. I consider being open and flexible to the advance of standards to be a sign of a mature programmer.
1. As for isError() and getError() they would return true/false or a string respectively. They are used to check if an error occurred by saving the error state after the query or fetch.
2. I prefer to always return an object from the query() method. It makes it consistent to deal with rather than it being either an object or false. Especially if you pass it, then the dependency is clean/clear.
3. I would recommend that you change the naming now. It is not a good practice become too familiar with you own non-standard naming. I consider being open and flexible to the advance of standards to be a sign of a mature programmer.
(#10850)
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
I think the do_something_here is the style they use for you to be able to differentiate between built-in functions and your own. (Correct me if I'm wrong.)scottayy wrote:IMO, the standard doSomethingHere is much uglier and harder to deal with thatn do_something_here, but eh, standards are standards, and who am I?
But yeah, I don't like have the first letter of a function lowercased... The standards I code by are most derived from C++, however typing the dollar sign made me stop using prefixes... made me press shift too often. :-p
So, how's this conform to the standards (besides documentation blocks)
Code: Select all
<?php
class db_connect
{
private $_host;
private $_username;
private $_password;
private $_linkId;
public function __construct($host, $username, $password)
{
$this->_host = $host;
$this->_username = $username;
$this->_password = $password;
}
public function connect()
{
$this->_linkId = mysql_connect($this->_host, $this->_username, $this->_password);
}
public function useDb($db)
{
mysql_select_db($db, $this->_linkId);
}
public function escape($var)
{
return mysql_real_escape_string($var, $this->_linkId);
}
public function query($sql)
{
if (!$this->_linkId) {
$this->connect();
}
if (is_resource($result = mysql_query($sql, $this->_linkId))) {
require_once getcwd() . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'db_result.class.php';
return new db_result($result);
}
return false;
}
}Code: Select all
<?php
class db_result
{
private $_result;
public function __construct($result)
{
$this->_result = $result;
}
public function numRows()
{
return mysql_num_rows($this->_result);
}
public function fetchRow($fetch_type=MYSQL_ASSOC)
{
return mysql_fetch_array($this->_result, $fetch_type);
}
}You asked for articles on some of the stuff in OO earlier, and since I'm in a rush I'll just post them if you're still interested:
http://hudzilla.org/phpwiki/index.php?t ... rogramming
Something I read yesterday (just started with OOP), I found the article quite good.
http://hudzilla.org/phpwiki/index.php?t ... rogramming
Something I read yesterday (just started with OOP), I found the article quite good.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I don't know if everyone will agree, but the query method
Should have no business requiring the file itself. Either that should be re factored into it's own method of the result class is simply lazy loaded. Probably the latter.
The next step would be supporting result operations for non-select's. For instance, currently numRows() and fetchRow() have no business for in update/insert operation. Polymorphism could be applied to retrieve the correct result class depending on the query itself.
Code: Select all
public function query($sql)
{
if (!$this->_linkId) {
$this->connect();
}
if (is_resource($result = mysql_query($sql, $this->_linkId))) {
require_once getcwd() . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'db_result.class.php';
return new db_result($result);
}
return false;
}The next step would be supporting result operations for non-select's. For instance, currently numRows() and fetchRow() have no business for in update/insert operation. Polymorphism could be applied to retrieve the correct result class depending on the query itself.
-
thinsoldier
- Forum Contributor
- Posts: 367
- Joined: Fri Jul 20, 2007 11:29 am
- Contact:
why does one class handle both connecting and querying and another class for the results?
see: http://bs.php.net/autoload
By the second page I definitely don't understand the need for the result to by handled by a separate class. Anyone able to explain why to me?[/syntax]
Code: Select all
require_once getcwd().DIRECTORY_SEPARATOR.'class'.DIRECTORY_SEPARATOR.'db_result.class.php';
return new db_result($this->link_id, $result); see: http://bs.php.net/autoload
By the second page I definitely don't understand the need for the result to by handled by a separate class. Anyone able to explain why to me?[/syntax]
-
thinsoldier
- Forum Contributor
- Posts: 367
- Joined: Fri Jul 20, 2007 11:29 am
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: